D
Dimitre Novatchev
I'm trying to copy a node and all its descendants and also change all the id
This is something very fundamental in XSLT.
Use the identity transform and override it for "id" attributes.
The solution looks like this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl
utput omit-xml-declaration="yes"/>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@id">
<xsl:attribute name="id">
<xsl:value-of select="concat(., '-1')"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="/*/chapter"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on your source.xml:
<book>
<chapter id="c1">
<verse id="v1">Hello World</verse>
<verse id="v2">Goodbye World</verse>
</chapter>
<biblio id="b1">
<ref>reference one</ref>
</biblio>
</book>
the wanted result is produced:
<chapter id="c1-1">
<verse id="v1-1">Hello World</verse>
<verse id="v2-1">Goodbye World</verse>
</chapter>
=====
Cheers,
Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
attributes.
<book>
<chapter id="c1">
<verse id="v1">Hello World</verse>
<verse id="v2">Goodbye World</verse>
</chapter>
<biblio id="b1">
<ref>reference one</ref>
</biblio>
</book>
I've been working on this little sample and can't get anywhere. I'd like to
be able to copy the chapter and make it output this:
<chapter id="c1-1">
<verse id="v1-1">Hello World</verse>
<verse id="v2-1">Goodbye World</verse>
</chapter>
This is something very fundamental in XSLT.
Use the identity transform and override it for "id" attributes.
The solution looks like this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@id">
<xsl:attribute name="id">
<xsl:value-of select="concat(., '-1')"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="/*/chapter"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on your source.xml:
<book>
<chapter id="c1">
<verse id="v1">Hello World</verse>
<verse id="v2">Goodbye World</verse>
</chapter>
<biblio id="b1">
<ref>reference one</ref>
</biblio>
</book>
the wanted result is produced:
<chapter id="c1-1">
<verse id="v1-1">Hello World</verse>
<verse id="v2-1">Goodbye World</verse>
</chapter>
=====
Cheers,
Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL