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
user394
  • 14,194
  • 21
  • 66
  • 93

1 Answers1

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.

slm
  • 363,520
  • 117
  • 767
  • 871