找到章节的最大深度 [英] finding maximum depth of chapter

查看:89
本文介绍了找到章节的最大深度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有人.在这种情况下,我要计算章节的最大深度.例如,一本没有章节的书的高度为0.一本书只有几章,没有章节,高度应为1.以下为xml:

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>

顺便说一句,我用了撒克逊语.我只想尝试使用匹配的模板.在这种情况下,输出为text,结果为

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

这是我的XSL,用于计算每个音符的深度吗?是的,然后如何通过调用名为max的模板来输出最大的电流?

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 >

推荐答案

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

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

这篇关于找到章节的最大深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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