从根开始的 XSL 计数节点 [英] XSL Count Nodes From Root

查看:23
本文介绍了从根开始的 XSL 计数节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 XML 是完全递归的,因为其中的每个元素都是一个项目",并且类型由类型"属性描述.在我的 XSL 中,我希望能够确定我处于迭代的哪个级别,或者换句话说,我当前处于从根开始的多少级别.我不知道该怎么做...

My XML is completely recursive in that every element within it is an "Item" and the type is delineated by a "type" property. In my XSL I want to be able to determine what level of iteration I am at, or in other words, how many levels from the root I am currently. I can't figure out how to do this . . .

XML 示例:

<Questionnaire>
    <Item ItemType="Group">
        <Caption>ABC</Caption>
        <Item ItemType="Group">
            <Caption>DEF</Caption>
            <Item ItemType="Question">
                <Caption>What's Wrong?</Caption>
            </Item>
        </Item>
    </Item>
    <Item ItemType="Group">
        <Caption>QRS</Caption>
        <Item ItemType="Group">
            <Caption>TUV</Caption>
            <Item ItemType="Question">
                <Caption>What's Wrong?</Caption>
            </Item>
        </Item>
        <Item ItemType="Group">
            <Caption>XYZ</Caption>
            <Item ItemType="Question">
                <Caption>What's Wrong?</Caption>
            </Item>
        </Item>
    </Item>
</Questionnaire>

XSL 示例:

<xsl:template match="/Questionnaire">
    <xsl:for-each select="Item">
        <fieldset>
            <legend><xsl:value-of select="Caption" /></legend>
            <xsl:call-template name="ItemTemplate" />
        </fieldset>         
    </xsl:for-each>
</xsl:template>

<xsl:template name="ItemTemplate">
    <xsl:if test="@ItemType != 'Question'">
        <ol>
            <xsl:for-each select="Item">
                <li>
                    <xsl:value-of select="Caption" />
                    <xsl:call-template name="ItemTemplate" />
                </li>
            </xsl:for-each>
        </ol>
    </xsl:if>
</xsl:template>

推荐答案

这个?

count(ancestor::Item)

顺便说一句,在 XSLT 中使用 更为惯用, 而不是命名模板和 .

By the way, it is more idiomatic in XSLT to use <xsl:template match=...> and <xsl:apply-templates>, rather than named templates and <xsl:call-template>.

这篇关于从根开始的 XSL 计数节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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