问候
如何找到最深的节点?因此,对于这个例子,String将是最深的节点:
我想要的结果是5
<org.olat.course.nodes.STCourseNode> 0 <ident>81473730700165</ident> 1 <type>st</type> <shortTitle>General Information</shortTitle> <moduleConfiguration> 2 <config> 3 <entry> 4 <string>allowRelativeLinks</string> 5 <--- <string>false</string> </entry> <entry> <string>file</string> <string>/kgalgemeneinformatie.html</string> </entry> <entry> <string>configversion</string> <int>3</int> </entry> <entry> <string>display</string> <string>file</string> </entry> </config> </moduleConfiguration> </org.olat.course.nodes.STCourseNode>
注意:我使用php,xpath
也欢迎其他可能性:)
问候
Dieter Verbeemen
使用XPath 2.0,您可以编写一个XPath表达式,我认为它是max(descendant::*[not(*)]/count(ancestor::*))
。使用XPath1.0,您可以找到以XSLT作为主机语言的节点,就像在中一样
<xsl:template match="/">
<xsl:for-each select="descendant::*[not(*)]">
<xsl:sort select="count(ancestor::*)" data-type="number" order="descending"/>
<xsl:if test="position() = 1">
<xsl:value-of select="count(ancestor::*)"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
如果您使用PHP作为XPath的"宿主"语言,您可能可以编写类似的内容,在descendant::*[not(*)]
上循环,元素没有任何子元素,并为每个元素计算count(ancestor::*)
并存储最大值。
〔edit〕这里有一些PHP的尝试:
$xpath = new DOMXPath($doc);
$leafElements = $xpath->query("descendant::*[not(*)]");
$max = 0;
foreach ($leafElements as $el) {
$count = $xpath->evaluate("count(ancestor::*)", $el);
if ($count > $max) {
$max = $count;
}
}
// now use $max here