Date: Tue, 09 Nov 2010 21:50:43 -0500
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
From: "G. Ken Holman" <gkholman(_at_)CraneSoftwrights(_dot_)com>
Subject: Re: [xsl] Returning a value from a template
Message-Id:
<7(_dot_)0(_dot_)1(_dot_)0(_dot_)2(_dot_)20101109211020(_dot_)023bec20(_at_)wheresmymailserver(_dot_)com>
At 2010-11-09 18:07 -0800, Narayan wrote:
I have been scratching my head on how to return a value from a called template
which includes a for loop. A snippet of the XML fragment is as follows:
<Loop-N1>
<Segment-N1>
<Element-98>BT</Element-98>
<Element-93 xsi:nil="true"/>
<Element-66>21</Element-66>
<Element-67>RA0129937</Element-67>
</Segment-N1>
</Loop-N1>
<Loop-N1>
<Segment-N1>
<Element-98>ST</Element-98>
<Element-93>ABC COLUMBUS DIVISION</Element-93>
<Element-66>11</Element-66>
<Element-67>RA0314562</Element-67>
</Segment-N1>
</Loop-N1>
The element <Loop-N1> can occur multiple times. If one of the <Element-66>'s
contain a value of "21" then I need to generate a fixed string value
else I need
to generate another fixed string literal. So I tried creating a named template
and calling this from my main template like a function:
<xsl:template name="getOrderSource">
<xsl:for-each select="/ns0:Transaction-850/ns0:Loop-N1">
<xsl:if test='ns0:Segment-N1/ns0:Element-66 = "21"'>
<xsl:variable name="ordrSrc" select="NAEDI"/>
</xsl:if>
</xsl:for-each>
<xsl:choose>
<xsl:when test='$orderSrc = "NAEDI"'>
<xsl:text disable-output-escaping="no">NAEDI</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text disable-output-escaping="no">NAEDIBrokerage</xsl:text>
(the default for d-o-e is "no" so you don't have to say it)
</xsl:otherwise>
</xsl:choose>
</xsl:template>
This was called like:
<corecom:ID>
<xsl:call-template name="getOrderSource"/>
</corecom:ID>
However, I realized that the variable declared inside the for-each is not
visible to the xsl:choose that follows.
Right. The scope of a local variable is the following siblings and
their descendants. There are no such following siblings.
What I was trying to do is to set a flag
to indicate that the element exists and then return the appropriate string
literal. Would there be a simpler way to do this?
Yes ... don't use the flag. Something along the lines of:
<xsl:template name="getOrderSource">
<xsl:for-each select="/ns0:Transaction-850/ns0:Loop-N1">
<xsl:choose>
<xsl:when test='ns0:Segment-N1/ns0:Element-66 = "21"'>NAEDI</xsl:when>
<xsl:otherwise>NAEDIBrokerage</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
I hope this helps.
. . . . . . . Ken
--
Contact us for world-wide XML consulting & instructor-led training
Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/
G. Ken Holman mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Legal business disclaimers: http://www.CraneSoftwrights.com/legal
------------------------------
Thanks Ken. The problem with the above approach is that if there are 3
Loop-N1's
then the output would be:
<corecom:ID>NAEDIBrokerageNAEDIBrokerageNAEDIBrokerage</corecom:ID>
Thats why I was trying to set a flag inside the loop and then return a Single
value.
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--