みんな 。この場合、章の最大の深さを計算したいと思います。たとえば、章のない本の高さは 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>
ちなみに私はサクソンを使っています。一致するテンプレートのみを使用したい.この場合、出力はテキストで、結果は
3
これは、各ノートの深さを計算するための私の XSL ですか? そうですか?では、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> </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 >