Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Convert empty value ("") to "0"

From: Martin Honnen <mahotrash@-----.-->
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/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent