Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Re: Convert empty value ("") to "0" >Thread Next - Re: Convert empty value ("") to "0" Re: Convert empty value ("") to "0"To: NULL Date: 8/26/2009 12:29:00 PM Savvoulidis Iordanis wrote:
> If the element is not empty then it should become
>
> <Coupon HomeTeamOdd="value">
> </Coupon>
>
> Only when empty, I want to replace the value with 0.
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.
--
Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
