I'm using xmllint --shell to inspect a very large XML file. I've written an XPath query that returns the results that I want, but in order to view and save the results, I'm having to cd to each node and then write filename.xml. This wouldn't be so bad if I didn't have to search each time, selecting the result index that I want. Example:

xpath count(/root/entry/subentry[special_id = /root/entry/subentry/special_id])
Object is a Node Set :
Set contains 121 nodes:
cd (/root/entry/subentry[special_id = /root/entry/subentry/special_id])[1]
write node1.xml
cd (/root/entry/subentry[special_id = /root/entry/subentry/special_id])[2]
write node2.xml
cd (/root/entry/subentry[special_id = /root/entry/subentry/special_id])[3]
write node3.xml
cd (/root/entry/subentry[special_id = /root/entry/subentry/special_id])[121]
write node121.xml

The above is more or less what I'm doing. Is there a way to save the XML nodes out as a file straight from the search without having to cd to them individually and continue to repeat the search? Or is there a way to preserve the search results, or get their position numbers in one query?

  • Doesn't look like xmllint --shell has variable assignment, so you'd either need to template the xmllint shell commands and feed those in on standard input, or instead write code against libxml xpath context or similar to select what you want. – thrig Nov 18 '15 at 0:57
  • Hmm. So maybe write up an XSLT or something? – jktravis Nov 18 '15 at 11:03

This doesn't seem to be possible, so I wrote up an XSLT to do it.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    <xsl:template match="/">
            <xsl:for-each select="/root/entry/subentry[special_id = /root/entry/subentry/special_id]">
                <xsl:copy-of select="."/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.