title: xpath query on Linux command line
body: <p>I am trying to use xpath queries on stack exchange data. The xml file has the following structure.</p>
  
  <pre><code>&lt;posts&gt;
      &lt;row Id="2" PostTypeId="1" Body="some text goes here" ..... /&gt;
  &lt;/posts&gt;
  </code></pre>
  
  <p>According to the data the row has the following attributes. It does not have any text in it.</p>
  
  <pre><code>   - Id
     - PostTypeId
        - 1: Question
        - 2: Answer
     - ParentID (only present if PostTypeId is 2)
     - AcceptedAnswerId (only present if PostTypeId is 1)
     - CreationDate
     - Score
     - ViewCount
     - Body
     - OwnerUserId
     - LastEditorUserId
     - LastEditorDisplayName="Jeff Atwood"
     - LastEditDate="2009-03-05T22:28:34.823"
     - LastActivityDate="2009-03-11T12:51:01.480"
     - CommunityOwnedDate="2009-03-11T12:51:01.480"
     - ClosedDate="2009-03-11T12:51:01.480"
     - Title=
     - Tags=
     - AnswerCount
     - CommentCount
     - FavoriteCount
  </code></pre>
  
  <p>I want to query this xml document using xpath query. For eg., /posts/row[@PostTypeId="1"]/@Body. That is extract all the body that is of PostTypeId="1" (questions). I tried the above query using the tool <a href="http://www.xmlme.com/XpathTool.aspx" rel="nofollow">http://www.xmlme.com/XpathTool.aspx</a>. It worked as expected of retrieving the Body of type questions from the xml document.</p>
  
  <p>However I tried in ubuntu using the following ways.</p>
  
  <ol>
  <li>libxml-xpath-perl - I executed xpath -e "/posts/row[@PostTypeId=\"1\"]/@Body" posts.xml. It is not extracting the Body attribute alone. Instead it is dumping the entire the row element.</li>
  <li><p>I wrote a python script myself using lxml.etree. The code is as follows.  </p>
  
  <p><code>tree = etree.parse(input file)
  print (tree.xpath(xpathexpression))</code></p></li>
  </ol>
  
  <p>I invoked the above python script with similar command line above. It said no matching nodes found.
  3. libxml2-utils - Tried xmllint --pattern "/posts/row[@PostTypeId=\"1\"]/@Body" posts.xml. It also complained saying no matching nodes found.</p>
  
  <p>Given the fact that xpath query works on the online tool but it is not working on the command line, I am on the humble opinion that entering the xpath query on the command line requires some special consideration. What is the right xpath query to be given when using command line tools in bash script on ubuntu?</p>