Question

I'd like to add the xml:id=foo attribute to the top-level <book> node in a DocBook file, using XSLT. 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>
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top