簡體   English   中英

XPath 的 string-length() 沒有子節點的節點

[英]XPath's string-length() of a node without its children

我需要一個單行 XPath 1.0 表達式來計算節點的長度,但不包括任何子節點中的文本(如果它們存在)。

例如,字符串長度()

<node>This is text</node>

是 12 (ok),但是 string-length() 的

<node>This is<any>any text</any> text</node>
<node>This <any>any text</any>is text</node>

大於 12(不行)。

我不能使用string-length(//node/text()) ,因為它需要node的第一個文本塊,如果node有任何子node

XPath 1.0

單獨的 XPath 1.0 不能僅提供元素的文本節點子節點的字符數。 您還必須使用托管 XPath 庫的語言的工具。

XPath 2.0 及更高版本

這個 XPath(首先由@MartinHonnen 在評論中發布),

sum(/node/text()/string-length())

將根據要求提供/node的所有文本節點子節點的字符數。

也可以看看

使用這個希望會為 1.0 運行:

string-length(//node)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM