【发布时间】:2011-05-12 01:27:42
【问题描述】:
每个人。在这种情况下,我想计算章节的最大深度。例如,没有章节的书的高度为 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 >
【问题讨论】:
-
哦,谁能帮帮我??请...
-
我不明白您想要生成的 exact 输出是什么。请说明。
-
好问题,+1。请参阅我的答案以获得完整、简短且简单的单行 XPath 2.0 解决方案。 :)
标签: xslt xslt-2.0 max depth xpath-2.0