2013-10-20 57 views
0

我想使用XSLT將xml:id=foo屬性添加到DocBook文件中的頂級<book>節點。我有一些工作,但我想知道是否有一個更簡單的方法來實現這一點。這是我目前的解決方案:XSLT:將xml:id屬性添加到頂級節點

<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

你可以縮短

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

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

但除此之外,你的做法是好的。而且您的版本可能更適合可讀性。