如何使用XPath选择子节点#text节点值最长的XML节点? [英] How do I select an XML node with the longest child #text node value with XPath?
问题描述
在使用此查询之前,我已经使用XPath选择了具有最大整数id值的节点:
I've used XPath to select the node with the largest integer id value before using this query:
//somenode[not(@id <= preceding::somenode/@id) and not(@id <= following::somenode/@id)]
我希望可以做类似的事情:
I was hoping that I could do something similar like:
//entry[not(string-length(child::text()) <= string-length(preceding::entry/child::text())) and not(string-length(child::text()) <= string-length(following::entry/child::text()))]
但是它返回一堆节点,而不是一个.
But it returns a bunch of nodes instead of just one.
示例XML:
<xml>
<entry>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</entry>
<entry>Nam dignissim mi a massa mattis rutrum eu eget mauris.</entry>
<entry>Ut at diam a sem scelerisque pretium nec pulvinar purus.</entry>
<entry>Nunc in nisi nec dolor accumsan suscipit vel a quam.</entry>
<entry>Nunc suscipit lobortis arcu, nec adipiscing libero bibendum nec.</entry>
<entry>Aenean eget ipsum et nunc eleifend scelerisque.</entry>
<entry>In eu magna et diam volutpat molestie.</entry>
<entry>In volutpat luctus mi, eu laoreet orci dictum vel.</entry>
<entry>In mattis mi nec magna sodales eu bibendum felis aliquet.</entry>
<!-- etc for 800 more lines or so -->
<entry>Duis auctor felis id neque gravida ut auctor ipsum ullamcorper.</entry>
<entry>Sed vel tortor mauris, et aliquet tellus.</entry>
</xml>
XPath测试: http://chris.photobooks.com/xml/default .htm?state = 1o
推荐答案
无法使用单个XPath 1.0表达式选择所需元素,因为在XPath 1.0中无法应用对所有选定节点的功能(string-length(someNodeSet)
仅应用于此节点集的第一个节点).另一个原因是,在XPath 1.0中,无法命名和引用范围变量.
The wanted element(s) cannot be selected with a single XPath 1.0 expression, because in XPath 1.0 it is not possible to apply a function to all selected nodes (string-length(someNodeSet)
is applied only on the first node of this node-set). Another reason is that in XPath 1.0 it isn't possible to name and reference range variables.
在XPath 2.0中,这很简单:
/*/entry[not(string-length(.) < /*/entry/string-length(.))]
上面选择了所有entry
个元素,这些元素的字符串值最大.
The above selects all entry
elements the length of whose string value is the maximal one.
/*/entry[not(string-length(.) < /*/entry/string-length(.))] [1]
上面选择了第一个(按文档顺序)诸如entry
元素.
The above selects the first (in document order) such entry
element.
基于XSLT 2.0的验证:
此转换:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:sequence select=
"/*/entry[not(string-length(.) < /*/entry/string-length(.))]"/>
</xsl:template>
</xsl:stylesheet>
应用于提供的XML文档:
<xml>
<entry>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</entry>
<entry>Nam dignissim mi a massa mattis rutrum eu eget mauris.</entry>
<entry>Ut at diam a sem scelerisque pretium nec pulvinar purus.</entry>
<entry>Nunc in nisi nec dolor accumsan suscipit vel a quam.</entry>
<entry>Nunc suscipit lobortis arcu, nec adipiscing libero bibendum nec.</entry>
<entry>Aenean eget ipsum et nunc eleifend scelerisque.</entry>
<entry>In eu magna et diam volutpat molestie.</entry>
<entry>In volutpat luctus mi, eu laoreet orci dictum vel.</entry>
<entry>In mattis mi nec magna sodales eu bibendum felis aliquet.</entry>
<!-- etc for 800 more lines or so -->
<entry>Duis auctor felis id neque gravida ut auctor ipsum ullamcorper.</entry>
<entry>Sed vel tortor mauris, et aliquet tellus.</entry>
</xml>
选择具有最大字符串长度的entry
元素(在这种情况下,只有一个)并输出所选元素:
selects the entry
elements (in this case only one) with the maximum string-length and outputs the selected elements:
<entry>Nunc suscipit lobortis arcu, nec adipiscing libero bibendum nec.</entry>
这篇关于如何使用XPath选择子节点#text节点值最长的XML节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!