I want :
<Coupon>
<HomeTeamOdd />
</Coupon>
to converted as
<Coupon HomeTeamOdd="0">
</Coupon>
Well what do you want to happen when the HomeTeamOdd element is not empty?
The following stylesheet
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Coupon">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="HomeTeamOdd"/>
<xsl:apply-templates select="node()[not(self::HomeTeamOdd)]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="HomeTeamOdd[not(node())]">
<xsl:attribute name="HomeTeamOdd">0</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
should do what you want for the input you have shown but somehow your
problem seems underspecified.
--
Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/
<Coupon HomeTeamOdd="value">
</Coupon>
Only when empty, I want to replace the value with 0.
(PS. Needless to say, as an XSLT newcomer, that I couldn't understand a bit
of what you posted ! I'd be glad, if you care to send me some rough
explanation.
Anyway thanks, I'll try to adjust it to my XSL file and see what happens).
Then you need
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Coupon">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="HomeTeamOdd"/>
<xsl:apply-templates select="node()[not(self::HomeTeamOdd)]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="HomeTeamOdd">
<xsl:attribute name="HomeTeamOdd">
<xsl:choose>
<xsl:when test="not(node())">0</xsl:when>
<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
> (PS. Needless to say, as an XSLT newcomer, that I couldn't understand a bit
> of what you posted ! I'd be glad, if you care to send me some rough
> explanation.
The template
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
alone performs an identity transformation by doing a shallow copy of
each node and then processing the attributes and child nodes with
matching templates. So as long as we only have above template we copy
the input to the output, level by level and node by node.
By adding additional templates like
<xsl:template match="Coupon">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="HomeTeamOdd"/>
<xsl:apply-templates select="node()[not(self::HomeTeamOdd)]"/>
</xsl:copy>
</xsl:template>
we override the identity transformation for selected nodes, in the above
case for 'Coupon' elements we also do a shallow copy and process the
attribute nodes but instead of processing all child nodes we first
process the HomeTeamOdd child element as we want to transform it into an
attribute and we can only do that as long as now child elements have
been added.
The template for 'HomeTeamOdd' elements is
<xsl:template match="HomeTeamOdd">
<xsl:attribute name="HomeTeamOdd">
<xsl:choose>
<xsl:when test="not(node())">0</xsl:when>
<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
and simply creates an attribute of that names and then checks whether
there is no contents (i.e. there aren't any child nodes), in that case 0
is output as the value of the attribute, otherwise the string value of
the element is output as the attribute value.