finding maximum depth of chapter

大憨熊 提交于 2019-12-13 15:24:04

问题


everyone . In this case ,I want to conpute the maxximun depth of the chapter.For instance, a book without chapters has height 0 . A book only has chapters with no sections ,the height should be 1.The folowing is 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>

By the way ,I used saxon. I want to try to use matching templates only.In this case ,the output is text ,and the result is

 3

This is my XSL for computing each note's depth? Is it right?And then how can out put the maximum of the curren by call a template named max??

<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:apply-templates select="book"/>
    <xsl:text>&#10;</xsl:text>
</xsl:template>

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

<xsl:template match="chapter|section">
    <xsl:param name="depth"  as="item()*"/>
    <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:


And here is your solution -- corrected:

<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 >



回答2:


XSLT 1.0 solution:

<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>

when this transformation is applied on the provided XML document:

<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>

the wanted, correct answer is produced:

3



回答3:


Use:

max(//(chapter|section)/count(ancestor::*[self::chapter or self::section]))+1

This can be made slightly more efficient:

max(//(chapter|section)[not(chapter or section)]
            /count(ancestor::*[self::chapter or self::section])
    ) +1


来源:https://stackoverflow.com/questions/5972390/finding-maximum-depth-of-chapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!