0

用于计算 xml 缩进深度的最简单的 xpath 2.0 是什么?我的变种还不聪明:

<xsl:param name="maxdepth" select="number(substring(concat(
'16'[count(current()/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*)>0],
'15'[count(current()/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*)>0],
'14'[count(current()/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*)>0],
'13'[count(current()/*/*/*/*/*/*/*/*/*/*/*/*/*/*)>0],
'12'[count(current()/*/*/*/*/*/*/*/*/*/*/*/*/*)>0],
'11'[count(current()/*/*/*/*/*/*/*/*/*/*/*/*)>0],
'10'[count(current()/*/*/*/*/*/*/*/*/*/*)>0],
'09'[count(current()/*/*/*/*/*/*/*/*/*)>0],
'08'[count(current()/*/*/*/*/*/*/*/*)>0],
'07'[count(current()/*/*/*/*/*/*/*)>0],
'06'[count(current()/*/*/*/*/*/*)>0],
'05'[count(current()/*/*/*/*/*)>0],
'04'[count(current()/*/*/*/*)>0],
'03'[count(current()/*/*/*)>0],
'02'[count(current()/*/*)>0],
'01'[count(current()/*)>0])
,1,2)
)"/>
4

1 回答 1

5

使用

max(//node()[not(node())]/count(ancestor-or-self::node()))

这将产生 XML 文档中所有叶节点深度的最大深度,包括/级别 1 的文档节点 ( )。

这是一个完整的例子

<xsl:stylesheet version="2.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>

 <xsl:template match="/">
     <xsl:sequence select=
      "max(//node()[not(node())]/count(ancestor-or-self::node()))"/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时:

<root>
   <x>This is:</x>
   <a>
      <b>
         <c>hello</c>
      </b>
   </a>
   <a>
      <b>
         <c1>world</c1>
      </b>
   </a>
   <a>
      <b>!</b>
   </a>
   <y>The End</y>
</root>

产生了想要的正确结果

6

更新

如果需要任何元素的最大深度,请使用几乎相同的 XPath 表达式:

max(//*[not(*)]/count(ancestor-or-self::*))
于 2012-08-24T14:28:24.060 回答