xmllint shell 未顯示計數 xpath 表達式中的數字?

xmllint shell 未顯示計數 xpath 表達式中的數字?

我在 cygwin 上使用 xmllint 的互動式 shell。我想計算節點的數量,但 xmllint 的行為似乎不符合我的預期:

$> 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

它說這是一個數字,但它並沒有以我在網路搜尋中看到的方式顯示該數字:

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

這裡出了什麼問題?

$> 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

答案1

我意識到@Stephane 在評論中提到了這一點,但我遇到了這種技術,並打算在看到他對相同解決方案的評論之前發布這個答案。不確定這樣做的禮儀,但這就是我發現的。如果在別人的評論上發布答案不合適,LMK 和我可以刪除這個答案。

解決方案

您需要使用xpathxmllint 中的命令來顯示從函數傳回的結果count

$ 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
/ > 

在這個網站上找到了答案:在 xmllint Shell 中使用 XPath 表達式

相關內容