XSLT:如何编写一个<xsl:apply-templates />以匹配具有模式和不具有模式的模板的节点,而又不影响顺序?

投降thakran

假设我有以下XML:

<root>
    <a>1</a>
    <b>2</b>
    <a>3</a>
    <c>4</c>
    <a>5</a>
    <a>6</a>
    <b>7</b>
    <a>8</a>
    <c>9</c>
</root>

考虑以下XSL:

<xsl:template match="root">
    <xsl:apply-templates select="a | b | c"/>    <!-- matches node 'b' with a non-mode template instead of the one with mode="test" -->
</xsl:template>

<xsl:template match="a">
    <xsl:value-of select="."/>
</xsl:template>

<xsl:template match="b">
    <xsl:text> ignore </xsl:text>
</xsl:template>

<xsl:template match="b" mode="test">
    <xsl:value-of select="."/>
</xsl:template>

<xsl:template match="c">
    <xsl:value-of select="."/>
</xsl:template>

我正在尝试编写XSL模板调用,该调用将根节点内的所有节点与其对应的模板b进行匹配,但该节点应与的模板进行匹配mode="test"节点处理的顺序不应受到干扰。

所需的输出是:

1
2
3
4
5
6
7
8
9
乔尔·兰森(Joel M.Lamsen)

我不知道这是否适用于您。代替

<xsl:template match="root">
    <xsl:apply-templates select="a | b | c"/>
</xsl:template>

<xsl:template match="root">
    <xsl:for-each select="*">
        <xsl:choose>
            <xsl:when test="name()='a'">
                <xsl:apply-templates select="."/>
            </xsl:when>
            <xsl:when test="name()='b'">
                <xsl:apply-templates select="." mode="test"/>
            </xsl:when>
            <xsl:when test="name()='c'">
                <xsl:apply-templates select="."/>
            </xsl:when>
        </xsl:choose>
    </xsl:for-each>
</xsl:template>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档