6

I am using xmllint's interactive shell on cygwin. I would like to count the number of nodes, but xmllint doesn't seem to behaving as I expect:

$> cat test.xml
<?xml version="1.0"?>
<result>
  <node>
    <item/>
    <item/>
    <item/>
    <item/>
    <item/>
  </node>
</result>
$> xmllint --shell test.xml
/ > cat count(/result/node/item)
count(/result/node/item) is a number

It says it's a number, but it doesn't show the number in a manner I've seen from web searches:

count(/result/node/item) is a number : 5

What is going wrong here?

$> xmllint --version
xmllint: using libxml version 20706
   compiled with: Threads Tree Output Push Reader Patterns Writer SAXv1 FTP HTTP DTDValid HTML Legacy C14N Catalog XPath XPointer XInclude Iconv ISO8859X Unicode Regexps Automata Expr Schemas Schematron Modules Debug Zlib
6

I realize @Stephane mentioned this in the comments but I came across this technique and was going to post this answer prior to seeing his comment with the same solution. Not sure of the etiquette for this, but here's what I found. LMK and I can delete this answer if it's inappropriate to post an answer on someone else's comment.

Solution

You need to use the xpath command within xmllint to display the result being returned from the count function.

$ xmllint --shell test.xml 
/ > cat         
<?xml version="1.0"?>
<result>
  <node>
    <item/>
    <item/>
    <item/>
    <item/>
    <item/>
  </node>
</result>

/ > xpath count(/result/node/item)
Object is a number : 5
/ > 

Found the answer here on this site: Playing With XPath Expressions in The xmllint Shell.

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.