Get xmllint to output xpath results \n-separated, for attribute selector

xmllint get attribute value
xpath new line in text
--xpath-node-separator
xmllint multiple xpath
xmllint --xpath example
xmllint --format
bash parse xml
install xmllint

How can I get xmllint to output multiple results of xpath selector for attributes "per line"?

Take this example:

  <?xml version="1.0" encoding="ISO-8859-1"?>
  <config>
          <tagX key1="value1 " key2=" value2"/>
          <tagY key3="value3" key4=" value4 "/>
  </config>


  $ xmllint example.xml --xpath "/config/*/@*"

The result is:

   key1="value1 " key2=" value2" key3="value3" key4=" value4 "

What I'd like to get is:

   key1="value1 "
   key2=" value2"
   key3="value3"
   key4=" value4 "

Would I need to split after even-numbered quote marks, or is there any neater way to do this?

There's a related question, about the same subject except it's about picking out contents of <tag>value</tag>, and not <tag attribute="value" />

You can try:

$ xmllint --shell inputfile <<< `echo 'cat /config/*/@*'`

You might need to grep the output, though, so as to filter the undesired lines.

xmllint problems to output lines - xml - html, How can I get xmllint to output multiple results of xpath selector for attributes "per line"? Take this example: <?xml version="1.0" encoding="ISO-8859-1"?>  Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

If it's an option, try using xmlstarlet instead:

xmlstarlet sel -t -v "/config/*/@*" example.xml

xmllint: separate xpath result with newline, Get xmllint to output xpath results \n-separated, for attribute selector. You can try: $ xmllint --shell inputfile <<< `echo 'cat /config/*/#*'` You might need to grep the output, though, so as to filter the undesired lines. If using the latest xmllint, results should have been separated by \n now. Best How To : Basically, you can use xpath predicate ([]) to filter element.Assume that your XML doesn't have namespace prefix involved, the following xpath will get hostname attribute from <node> element having descendant element <database>:

The question is old but as I came to this post searching a solution to the same problem, here is my solution

On linux add sed substitution to split output:

$ xmllint example.xml --xpath "/config/*/@*" | sed "s| key|\nkey|g"

of course the substitution expression depends on your xml structure and your xpath query.

And you can even add line numbers on each line if you add nl

$ xmllint example.xml --xpath "/config/*/@*" | sed "s| key|\nkey|g" | nl

Which gives

   1 key1="value1 "
   2 key2=" value2"
   3 key3="value3"
   4 key4=" value4 "

xmllint: command line XML tool - Linux Man Pages (1), xmllint is a helpful tool for anazing xml files. one common usageis its xpath query, which can extract useful information from a xml file. we can extract all x coordinates using xpath: xmllint --xpath "//point/@x" input.xml. output: can be quite large and it won't be obvious how many results there are. plus,  Get xmllint to output xpath results -separated, for attribute selector. How can I get xmllint to output multiple results of xpath selector for attributes "per

I found that Ubuntu v.14 and v.18 use too old libxml2 versions. So i solved the issue mentioned in the topic by the recompiling of libxml2 v2.9.10.

1) Download the sources of libxml2 from http://linuxfromscratch.org/blfs/view/cvs/general/libxml2.html

2) Download the test suite:

$ wget http://www.w3.org/XML/Test/xmlts20130923.tar.gz

3) Execute:

$ tar xf libxml2-2.9.10.tar.gz

4) Execute:

$ cd libxml2-2.9.10/

5) Execute:

$ sed -i 's/test.test/#&/' python/tests/tstLastError.py

6) Execute:

$ export CPATH="/usr/include/python3.6/:$CPATH"

7) Execute:

$ sudo apt-get install -y python3-libxml2 python3-pip

8) Execute:

$ sudo pip3 install libxml2dom

9) Execute:

$ ./configure --prefix=/usr --disable-static --with-history --with-python=/usr/bin/python3 && make

Ensure here that the returning code ($?) is 0.

10) Execute:

$ tar xf ../xmlts20130923.tar.gz

11) Execute:

make check > check.log

Ensure here that the returning code ($?) is 0.

12) Execute:

$ sudo make install

Enjoy

$ xmllint data.xml -xpath "//some_child/ancestor::some_parent/attribute::id"                                                                               
 id="1"
 id="10010"
 id="10011"
 id="10020"
 id="10021"

Xml grep, Read xmllint man page on Linux: $ man 1 xmllint Parse a file and output an annotated tree of the in-memory version of the document. Fetch external DTD and populate the tree with inherited attributes. Note that this works for full document not fragments or result from XPath queries. Output results as an HTML file. XPath uses path expressions to select nodes in an XML document. The node is selected by following a path or steps. The most useful path expressions are listed below: Selects all nodes with the name " nodename " Selects from the root node. Selects nodes in the document from the current node that match the selection no matter where they are.

If using the latest xmllint, results should have been separated by \n now. However, if fields contain \n, you can use this patched version to use \0 as a separator, with --xpath0. For whatever reason the PR hasn't been merged yet.

lxml FAQ, xmllint --xpath '/dictionary/pardefs/pardef[@n="gen__apos"]' To get all attributes of the e element that has the lm "cake": $ xmllint If nothing else, it's worth getting because it can append newlines after each selection. XML Starlet requires -a -few -more -options, but can output newlines after each match: To make it easier: Test your XPath expressions in the internal xmllint shell! Using Easy XPath Expressions. Generally, xmllint is known as a popular tool to validate your XML structure. Mostly unknown is its internal shell. With this shell you can make some spiffy XPath tests and check if it returns exactly what you want.

[PDF] PDF documentation, Why can't lxml parse my XML from unicode strings? or clear the root node in iterparse()?; How do I output null characters in XML text? How can I sort the attributes? What are the findall() and xpath() methods on Element(Tree)?; Why doesn't come with their own command line frontends, namely xmllint and xsltproc. --help Print out a short usage summary for xmllint. --html Use the HTML parser. --htmlout Output results as an HTML file. This causes xmllint to output the necessary HTML tags surrounding the result tree output so the results can be displayed/viewed in a browser. --insert Test for valid insertions.

xmllint - man pages section 1: User Commands, Why doesn't the pretty_print option reformat my XML output? Why can't lxml parse my XML from unicode strings? What are the findall() and xpath() methods on Element(Tree)? . Note that the C parser benchmark results are based on xmlbench, Python attribute access to traverse the XML tree. The output of xmllint is correct. The text nodes containing the newlines are not selected by the given XPath expression. To add newlines after each matched node you have to use a different

Bash xpath, xmllint - command line XML tool. xmllint [--version | --debug | --shell | --xpath "​XPath_expression" --debug Parse a file and output an annotated tree of the in-​memory version of the --dtdattr Fetch external DTD and populate the tree with inherited attributes. --htmlout Output results as an HTML file. Attribute Description; id: Optional. Specifies a unique ID for the element: xpath: Required. Specifies an XPath expression, relative to the element being declared, that identifies the child elements to which the identity constraint applies: any attributes: Optional. Specifies any other attributes with non-schema namespace

Comments
  • Does this help -- echo 'cat /config/*/@*[starts-with(name(),"key")]' | xmllint --shell input.xml
  • @devnull: | grep = and it's fine. (actually, no [starts-with()... just /@* I used "key1, key2..." as metasyntactic variables, the actual attribute names would be arbitrary.)
  • Yes, grep -v and done!
  • @devnull: I'd prefer inclusive grep (all desired lines contain =, and undesired are rather fixed: / > cat /config/*/@* and ` -------` - and it's possible the value might contain -------, so grep '=' is sure to find what I want (unless I use = in the selector, which I don't.)
  • @devnull: By the way, post that as an answer and I'll accept.
  • The here-doc redirect is weird, it should be enough to say <<<'cat /config/*/@*'
  • Agree with Guss. Also I could work around parsing the diagnostics and the separator lines (with a different XPATH expression in my case), xmllint --shell /tmp/tt.xml <<< "cat //s/text()" | { read -r diag; while { read -r separator; read -r line; } ; do echo "${line}" ; done ; } > /tmp/tt-words.txt
  • The problem of this is that it depends on consistent naming scheme of the keys (properties). If they are not 'key1, key2' but 'name, birthdate', this won't work.
  • the commit link is dead
  • @kbolino moved