XSLT:将xml:id属性添加到顶级节点

时间:2013-10-20 02:12:53

标签: xml xslt docbook

我想使用XSLT将xml:id=foo属性添加到DocBook文件中的顶级<book>节点。我有一些工作,但我想知道是否有更简单的方法来实现这一点。这是我目前的解决方案:

<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
                xmlns:db='http://docbook.org/ns/docbook'
                version='1.0'>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="db:book">
    <xsl:copy>
        <xsl:attribute name="xml:id">
            <xsl:text>foo</xsl:text>
        </xsl:attribute>
            <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

你可以缩短

    <xsl:attribute name="xml:id">
        <xsl:text>foo</xsl:text>
    </xsl:attribute>

    <xsl:attribute name="xml:id">foo</xsl:attribute>

但除此之外你的方法还可以。为了便于阅读,您的版本可能更受欢迎。

相关问题