如何找到最深的节点(步骤)-Xpath-php-xml- [英] How do you find the deepest node (steps) - Xpath - php - xml -

查看:88
本文介绍了如何找到最深的节点(步骤)-Xpath-php-xml-的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


问候

如何找到最深的节点? 因此,在此示例中,String将是最深的节点:

How do you find the deepest node? So for this example, String would be the deepest node:

,我想要的结果是 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



Note: I use php, xpath

也欢迎其他可能性:)

亲切的问候

Dieter Verbeemen

Dieter Verbeemen

推荐答案

使用XPath 2.0,我可以编写一个单独的XPath表达式,如max(descendant::*[not(*)]/count(ancestor::*)).使用XPath 1.0,您可以找到以XSLT作为宿主语言的节点,如

With XPath 2.0 you could write a single XPath expression I think, as max(descendant::*[not(*)]/count(ancestor::*)). With XPath 1.0 you could find the node with XSLT as the host language as in

<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::*)并存储最大值值.

If you use PHP as the "host" language for XPath you can probably write something similar with a loop over descendant::*[not(*)], the elements not having any child elements, and computing count(ancestor::*) for each of them and storing the maximum value.

[edit]这是一些使用PHP的尝试:

[edit] Here is some attempt at 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

这篇关于如何找到最深的节点(步骤)-Xpath-php-xml-的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆