adding attributes to xml output
Posted: Wed Jan 09, 2008 3:52 am
Hello,
I want to add attributes to all elements called <outil> that are children of the rootelement <Listeoutils>. I want to copy all elements and attributes of the source xml document via the xslt stylesheet into an output xml document. then I want to add to all <outil> elements that have an attribute called "bibliothèque" a new attribute with the name "id_type" that has the value "1". I wrote this code but it doesn't work: Where is the mistake?
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8"/>
<xsl:preserve-space elements="*"/>
<xsl:template match="/">
<xsl:copy>
<xsl:copy-of select="/"/>
<xsl:apply-templates select="//outil"/>
</xsl:copy>
</xsl:template>
<xsl:template match="outil">
<xsl:for-each select="outil">
<xsl:choose>
<xsl:when test="@type='bibliothèque'">
<outil>
<xsl:attribute name="id_type">1</xsl:attribute>
</outil>
</xsl:when>
<xsl:otherwise>
<outil>
<xsl:attribute name="id_type">100</xsl:attribute>
</outil>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
I want to add attributes to all elements called <outil> that are children of the rootelement <Listeoutils>. I want to copy all elements and attributes of the source xml document via the xslt stylesheet into an output xml document. then I want to add to all <outil> elements that have an attribute called "bibliothèque" a new attribute with the name "id_type" that has the value "1". I wrote this code but it doesn't work: Where is the mistake?
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8"/>
<xsl:preserve-space elements="*"/>
<xsl:template match="/">
<xsl:copy>
<xsl:copy-of select="/"/>
<xsl:apply-templates select="//outil"/>
</xsl:copy>
</xsl:template>
<xsl:template match="outil">
<xsl:for-each select="outil">
<xsl:choose>
<xsl:when test="@type='bibliothèque'">
<outil>
<xsl:attribute name="id_type">1</xsl:attribute>
</outil>
</xsl:when>
<xsl:otherwise>
<outil>
<xsl:attribute name="id_type">100</xsl:attribute>
</outil>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>