Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 查找章节的最大深度_Xslt_Xslt 2.0_Max_Depth_Xpath 2.0 - Fatal编程技术网

Xslt 查找章节的最大深度

Xslt 查找章节的最大深度,xslt,xslt-2.0,max,depth,xpath-2.0,Xslt,Xslt 2.0,Max,Depth,Xpath 2.0,各位。在这种情况下,我想计算章节的最大深度。例如,一本没有章节的书的高度为0。一本书只有章节,没有章节,高度应为1。以下为xml: <book title="D"> <author> <name>abc</name> </author> <chapter title="chapter1"> <section title="section1.1"/> <section title="sectio

各位。在这种情况下,我想计算章节的最大深度。例如,一本没有章节的书的高度为0。一本书只有章节,没有章节,高度应为1。以下为xml:

<book title="D">
<author>
  <name>abc</name>
</author>

<chapter title="chapter1">
  <section title="section1.1"/>
  <section title="section1.2">
    <section title="section1.2.1"/>
<section title="section1.2.2"/>
  </section>
  <section title="section1.3">
<section title="section1.3.1"/>
  </section>
</chapter>

<chapter title="chapter2"/>

</book>
这是我用来计算每个音符深度的XSL?是吗?那么如何通过调用一个名为max的模板来输出最大值呢??




使用

max(//(chapter|section)/count(ancestor::*[self::chapter or self::section]))+1
max(//(chapter|section)[not(chapter or section)]
            /count(ancestor::*[self::chapter or self::section])
    ) +1
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/">
     <xsl:apply-templates select="//*[self::chapter or self::section]">
       <xsl:sort data-type="number" order="descending" select=
        "count(ancestor::*[self::chapter or self::section])
        "/>
     </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="chapter|section">
    <xsl:if test="position()=1">
     <xsl:value-of select=
     "count(ancestor::*[self::chapter or self::section]) +1
     "/>
    </xsl:if>
 </xsl:template>
</xsl:stylesheet>
<xsl:transform version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output method="text" encoding="UTF-8"/>

 <xsl:template match="/">
  <xsl:variable name="vDepths" as="xs:integer*">
   <xsl:apply-templates select="book"/>
  </xsl:variable>

  <xsl:sequence select="max($vDepths)"/>
 </xsl:template>

 <xsl:template match="book" as="xs:integer*">
  <xsl:apply-templates select="chapter">
   <xsl:with-param name ="depth" select ="1"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="chapter|section" as="xs:integer*">
  <xsl:param name="depth"  as="xs:integer"/>
  <xsl:variable name ="current" select ="$depth"/>

  <xsl:sequence select ="$depth"/>
  <xsl:if test ="not(empty(section))">
   <xsl:apply-templates select="section">
    <xsl:with-param name="depth" select="$depth+1"/>
   </xsl:apply-templates>
  </xsl:if>
 </xsl:template>
</xsl:transform >
这可以稍微提高效率

max(//(chapter|section)/count(ancestor::*[self::chapter or self::section]))+1
max(//(chapter|section)[not(chapter or section)]
            /count(ancestor::*[self::chapter or self::section])
    ) +1
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/">
     <xsl:apply-templates select="//*[self::chapter or self::section]">
       <xsl:sort data-type="number" order="descending" select=
        "count(ancestor::*[self::chapter or self::section])
        "/>
     </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="chapter|section">
    <xsl:if test="position()=1">
     <xsl:value-of select=
     "count(ancestor::*[self::chapter or self::section]) +1
     "/>
    </xsl:if>
 </xsl:template>
</xsl:stylesheet>
<xsl:transform version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output method="text" encoding="UTF-8"/>

 <xsl:template match="/">
  <xsl:variable name="vDepths" as="xs:integer*">
   <xsl:apply-templates select="book"/>
  </xsl:variable>

  <xsl:sequence select="max($vDepths)"/>
 </xsl:template>

 <xsl:template match="book" as="xs:integer*">
  <xsl:apply-templates select="chapter">
   <xsl:with-param name ="depth" select ="1"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="chapter|section" as="xs:integer*">
  <xsl:param name="depth"  as="xs:integer"/>
  <xsl:variable name ="current" select ="$depth"/>

  <xsl:sequence select ="$depth"/>
  <xsl:if test ="not(empty(section))">
   <xsl:apply-templates select="section">
    <xsl:with-param name="depth" select="$depth+1"/>
   </xsl:apply-templates>
  </xsl:if>
 </xsl:template>
</xsl:transform >

XSLT 1.0解决方案

max(//(chapter|section)/count(ancestor::*[self::chapter or self::section]))+1
max(//(chapter|section)[not(chapter or section)]
            /count(ancestor::*[self::chapter or self::section])
    ) +1
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/">
     <xsl:apply-templates select="//*[self::chapter or self::section]">
       <xsl:sort data-type="number" order="descending" select=
        "count(ancestor::*[self::chapter or self::section])
        "/>
     </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="chapter|section">
    <xsl:if test="position()=1">
     <xsl:value-of select=
     "count(ancestor::*[self::chapter or self::section]) +1
     "/>
    </xsl:if>
 </xsl:template>
</xsl:stylesheet>
<xsl:transform version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output method="text" encoding="UTF-8"/>

 <xsl:template match="/">
  <xsl:variable name="vDepths" as="xs:integer*">
   <xsl:apply-templates select="book"/>
  </xsl:variable>

  <xsl:sequence select="max($vDepths)"/>
 </xsl:template>

 <xsl:template match="book" as="xs:integer*">
  <xsl:apply-templates select="chapter">
   <xsl:with-param name ="depth" select ="1"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="chapter|section" as="xs:integer*">
  <xsl:param name="depth"  as="xs:integer"/>
  <xsl:variable name ="current" select ="$depth"/>

  <xsl:sequence select ="$depth"/>
  <xsl:if test ="not(empty(section))">
   <xsl:apply-templates select="section">
    <xsl:with-param name="depth" select="$depth+1"/>
   </xsl:apply-templates>
  </xsl:if>
 </xsl:template>
</xsl:transform >

这是您的解决方案--已更正

max(//(chapter|section)/count(ancestor::*[self::chapter or self::section]))+1
max(//(chapter|section)[not(chapter or section)]
            /count(ancestor::*[self::chapter or self::section])
    ) +1
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/">
     <xsl:apply-templates select="//*[self::chapter or self::section]">
       <xsl:sort data-type="number" order="descending" select=
        "count(ancestor::*[self::chapter or self::section])
        "/>
     </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="chapter|section">
    <xsl:if test="position()=1">
     <xsl:value-of select=
     "count(ancestor::*[self::chapter or self::section]) +1
     "/>
    </xsl:if>
 </xsl:template>
</xsl:stylesheet>
<xsl:transform version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output method="text" encoding="UTF-8"/>

 <xsl:template match="/">
  <xsl:variable name="vDepths" as="xs:integer*">
   <xsl:apply-templates select="book"/>
  </xsl:variable>

  <xsl:sequence select="max($vDepths)"/>
 </xsl:template>

 <xsl:template match="book" as="xs:integer*">
  <xsl:apply-templates select="chapter">
   <xsl:with-param name ="depth" select ="1"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="chapter|section" as="xs:integer*">
  <xsl:param name="depth"  as="xs:integer"/>
  <xsl:variable name ="current" select ="$depth"/>

  <xsl:sequence select ="$depth"/>
  <xsl:if test ="not(empty(section))">
   <xsl:apply-templates select="section">
    <xsl:with-param name="depth" select="$depth+1"/>
   </xsl:apply-templates>
  </xsl:if>
 </xsl:template>
</xsl:transform >


噢,谁能帮我?请……我不明白您想要的确切产量是多少。请详细说明。好问题,+1。请参阅我的答案,了解完整、简短且简单的一行XPath 2.0解决方案。:)谢谢。现在,我想你已经知道这个问题了?@ZAWD:我提供了两种不同的解决方案,完全解决了你的问题。请考虑接受和投票。谢谢你。现在,我想你已经知道这个问题了。如果我想输出每一个音符的深度,我的XSL中有什么不对吗?我的意思是,例如,第一章->“1”,段落11->“2”,分段1.2>“2”,段落1.2.1->“3”…所以我的XSL的输出是1 2 2 3 3 2 3 1。我想要的是结合模板马赫数和最大模板数(可以选择这个序列的最大数量“1 2 3 2 3 1”)@ZAWD:我在一个单独的答案中对你的解决方案进行了更正。@Dimitre Novatchev:是的,你给了我一个非常好的答案。但我不想使用,只使用一个名为max的模板(或其他什么),调用它来查找最大值。它会起作用吗?@ZAWD:我已经为您提供了一个仅使用模板的XSLT 1.0解决方案。您可以在XSLT 2.0中使用相同的解决方案。@ZAWD:我还在另一个答案中更正了您的解决方案。你找到了吗?