Reputation: 1
I need to get the final output structure xml as below for my project. but I'm facing few issues with this code.
<Endorsement>
<TL>STNDBY/CHG FEE/NO RFND/</TL>
<TL>CXL BY FLT DT/</TL>
</Endorsement>
<LText TopicId="MISCELLANEOUS">
<TL>01 NVB28JUL/NVA28JUL</TL>
</LText>
Here is the input xml file
<InfoMsg>
<AppNum>0</AppNum>
<MsgType>2</MsgType>
<Text>LAST DATE TO PURCHASE TICKET: 06JUN13</Text>
</InfoMsg>
<InfoMsg>
<UniqueKey>0001</UniqueKey>
<Lang>0</Lang>
<Text>E-TKT REQUIRED</Text>
</InfoMsg>..............
To achieve above output structure OI'musing the below piece of code
<xsl:for-each select="../InfoMsg">
<xsl:choose>
<xsl:when test="MsgType='01' or MsgType='1'">
<xsl:element name="Endorsement">
<xsl:element name="TL">
<xsl:value-of select="Text"/>
</xsl:element>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="LText">
<xsl:attribute name="TopicId">
<xsl:text>MISCELLANEOUS</xsl:text>
</xsl:attribute>
<xsl:element name="TL">
<xsl:value-of select="Text"/>
</xsl:element>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
Logic here is, for each Info Msg tag it should check if MSgType is eqaul to 01 or 1, if yes then it create Endorsement tag otherwise it should construct Ltext tag.Condition is The Msgtype used in Endorsement element should not get repeated in Ltext tag.
But it gives me below output
- <Endorsement>
<TL>STNDBY/CHG FEE/NO RFND/</TL>
</Endorsement>
- <Endorsement>
<TL>CXL BY FLT DT/</TL>
</Endorsement>
- <LText TopicId="MISCELLANEOUS">
<TL>01 NVB28JUL/NVA28JUL</TL>
</LText>
- <LText TopicId="MISCELLANEOUS">
<TL>02 NVB28JUL/NVA28JUL</TL>
</LText>
- <LText TopicId="MISCELLANEOUS">
<TL>LAST DATE TO PURCHASE TICKET: 26JUN13</TL>
</LText>
- <LText TopicId="MISCELLANEOUS">
<TL>E-TKT REQUIRED</TL>
</LText>
- <LText TopicId="MISCELLANEOUS">
<TL>ADDITIONAL TAXES, SURCHARGES, OR FEES MAY APPLY</TL>
</LText>
Here Endorsement tag is coming multiple times which is not necessary. It should come only once.
Pls suggest what changes can i make in my code to get it. Is there any alternate method or way of doing this.
Thanks Asma
Upvotes: 0
Views: 786
Reputation: 70638
At the moment, you are outputting an Endorsement element (or LText element) for each InfoMsg element you have, and so you get the repeated elements as you show.
What you need to be doing is just outputting the Endorsement element just once, and then selecting the relevant InfoMsg elements within this.
<Endorsement>
<xsl:for-each select="InfoMsg[MsgType='01' or MsgType='1']">
<TL>
<xsl:value-of select="Text"/>
</TL>
</xsl:for-each>
</Endorsement>
And similarly for Ltext.
Note there is no real need to use xsl:element here, just output the element directly.
In fact, you should probably be using template matching here, to allow you to re-use code. Try this XSLT instead:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<Endorsement>
<xsl:apply-templates select="InfoMsg[MsgType='01' or MsgType='1']" />
</Endorsement>
<LText TopicId="MISCELLANEOUS">
<xsl:apply-templates select="InfoMsg[not(MsgType='01' or MsgType='1')]" />
</LText>
</xsl:template>
<xsl:template match="InfoMsg">
<TL>
<xsl:value-of select="Text"/>
</TL>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0