how to default a value for non existing node in xslt -
i have following xml element in source , map element in target.
whenever there no node in source, want default string "none"
--------------------------source xml----------------------------------------------
<?xml version="1.0" encoding="utf-8"?> <instructions instructiontype="gen">some message</instructions> <instructions instructiontype="test">some other message</instructions>
---------------------------transformation xsl--------------------------------------
<xsl:if test='/ns1:orderresponse/ns1:orderresponsebody/ns1:orderresponseproperties/ns1:instructions/@instructiontype = "gen"'> <xsl:choose> <xsl:when test='/ns1:orderresponse/ns1:orderresponsebody/ns1:orderresponseproperties/ns1:instructions[@instructiontype = "gen"] != ""'> <ns0:sigen> <xsl:value-of select='substring(/ns1:orderresponse/ns1:orderresponsebody/ns1:orderresponseproperties/ns1:instructions[@instructiontype = "gen"],1.0,199.0)'/> </ns0:sigen> </xsl:when> <xsl:when test="not(/ns1:orderresponse/ns1:orderresponsebody/ns1:orderresponseproperties/ns1:instructions[@instructiontype = 'gen'])"> <ns0:sigen> <xsl:text disable-output-escaping="no">none</xsl:text> </ns0:sigen> </xsl:when> <xsl:otherwise> <ns0:sigen> <xsl:text disable-output-escaping="no">none</xsl:text> </ns0:sigen> </xsl:otherwise> </xsl:choose> </xsl:if>
---------------------------------------------issue-------------------------------------------------------------
when source xml doesn't have the node @ shown below (commented), cannot default value "none" target element "ns0:sigen"
<!--instructions instructiontype="gen">some message</instructions--> <instructions instructiontype="test">some other message</instructions>
i dont understand why below condition not working:
<xsl:when test="not(/ns1:orderresponse/ns1:orderresponsebody/ns1:orderresponseproperties/ns1:instructions[@instructiontype = 'gen'])">
please advise.
thanks
yogi
the first line in xslt sample says this...
<xsl:if test='/ns1:orderresponse/ns1:orderresponsebody/ns1:orderresponseproperties/ns1:instructions/@instructiontype = "gen"'>
i.e. testing if there instructions element has instructiontype equal "gen". so, if comment instructions element, statement false, , xsl:choose inside statement not executed.
to simplify things, consider putting instructions element in variable
<xsl:variable name="gen" select="ns1:orderresponse/ns1:orderresponsebody/ns1:orderresponseproperties/ns1:instructions[@instructiontype = 'gen']" />
then can have simple xsl:choose test this
<ns0:sigen> <xsl:choose> <xsl:when test="$gen != ''"><xsl:value-of select="$gen" /></xsl:when> <xsl:otherwise>none</xsl:otherwise> </xsl:choose> <ns0:sigen>
Comments
Post a Comment