在使用此查询之前,我已使用XPath选择具有最大整数id值的节点:
//somenode[not(@id <= preceding::somenode/@id) and not(@id <= following::somenode/@id)]
我希望我能做类似的事情:
//entry[not(string-length(child::text()) <= string-length(preceding::entry/child::text())) and not(string-length(child::text()) <= string-length(following::entry/child::text()))]
但是它返回了一堆节点而不是一个节点。
示例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
答案 0 :(得分:2)
无法使用单个XPath 1.0表达式选择所需元素,因为在XPath 1.0中无法将函数应用于所有选定节点(string-length(someNodeSet)
已应用仅在此节点集的第一个节点上)。另一个原因是在XPath 1.0中,无法命名和引用范围变量。
在XPath 2.0中,这是微不足道的:
/*/entry[not(string-length(.) < /*/entry/string-length(.))]
以上选择所有entry
元素,其字符串值的长度为最大值。
/*/entry[not(string-length(.) < /*/entry/string-length(.))] [1]
以上选择第一个(按文档顺序)这样的entry
元素。
基于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
元素(在这种情况下只有一个)并输出所选元素:
<entry>Nunc suscipit lobortis arcu, nec adipiscing libero bibendum nec.</entry>