[英]XSLT: Add an xml:id attribute to the top-level node
I'd like to add the xml:id=foo
attribute to the top-level <book>
node in a DocBook file, using XSLT. 我想使用XSLT将
xml:id=foo
属性添加到DocBook文件中的顶级<book>
节点。 I've got something working, but I was wondering if there was a simpler way to implement this. 我有一些工作,但我想知道是否有更简单的方法来实现这一点。 Here's my current solution:
这是我当前的解决方案:
<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>
You could shorten 你可以缩短
<xsl:attribute name="xml:id">
<xsl:text>foo</xsl:text>
</xsl:attribute>
to 至
<xsl:attribute name="xml:id">foo</xsl:attribute>
But other than that your approach is fine. 但是除此之外,您的方法还可以。 And your version might be preferred for readability.
并且您的版本可能更易读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.