 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 20th, 2010, 09:10 AM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Mutiple tags in XSLT
Hi Frnds,
Could any one please help me in writing XSLT for the below input and output xmls
input xml :
<LMPRequests>
<LMPRequest>
<LMPMessage>Details
<Section>Criteria
<Row>
<REGION_CODE>524002</REGION_CODE>
<STATUS>Y</STATUS>
<AMOUNT1>25.00></AMOUNT1>
<AMOUNT2>125.00></AMOUNT2>
<AMOUNT3>125.00></AMOUNT3>
<CODE1>A123></CODE1>
<CODE2>CB12></CODE2>
<CODE3>jk12></CODE3>
<UNIT1>A></UNIT1>
<UNIT2>D></UNIT2>
<UNIT3>C</UNIT3>
</Row>
</Section>
</LMPMessage>
</LMPRequest>
</LMPRequests>
Output XML should be like:
<?xml version="1.0" encoding="UTF-8"?>
<CreateClient xmlns="http://ufus.com/us/webservices">
<criteria>
<regioncode>524002</regioncode>
<Status>Y</Status>
<Assets>
<ClientAsset>
<Code>A123</Code>
<Unit>A</Unit>
<Amount>25.00</Amount>
</ClientAsset>
<ClientAsset>
<Code>CB12</Code>
<Unit>D</Unit>
<Amount>125.00</Amount>
</ClientAsset>
<ClientAsset>
<Code>jk12</Code>
<Unit>C</Unit>
<Amount>125.00</Amount>
</ClientAsset>
</Assets>
</criteria>
</CreateClient>
Could any one please guide me in writing XSLT
Thanks
|
|

April 20th, 2010, 09:23 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Here is an XSLT 1.0 stylesheet that should what you want:
Code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://ufus.com/us/webservices"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<CreateClient>
<criteria>
<regioncode>524002</regioncode>
<Status>Y</Status>
<Assets>
<xsl:for-each select="descendant::*[starts-with(local-name(), 'AMOUNT')]">
<xsl:variable name="pos" select="position()"/>
<ClientAsset>
<Code><xsl:value-of select="../*[local-name() = concat('CODE', $pos)]"/></Code>
<Unit><xsl:value-of select="../*[local-name() = concat('UNIT', $pos)]"/></Unit>
<Amount><xsl:value-of select="../*[local-name() = concat('AMOUNT', $pos)]"/></Amount>
</ClientAsset>
</xsl:for-each>
</Assets>
</criteria>
</CreateClient>
</xsl:template>
</xsl:stylesheet>
Please consider to properly indent and mark up ( http://p2p.wrox.com/misc.php?do=bbcode#code) your XML samples next time to make them more readable.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|

April 20th, 2010, 09:48 AM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Hi Martin,
Thanks for the response.
I am getting extra characters i.e >
output xml:
<?xml version="1.0" encoding="UTF-8"?>
<CreateClient xmlns="http://ufus.com/us/webservices">
<criteria>
<regioncode>524002</regioncode>
<Status>Y</Status>
<Assets>
<ClientAsset>
<Code>A123></Code>
<Unit>A></Unit>
<Amount>25.00></Amount>
</ClientAsset>
<ClientAsset>
<Code>CB12></Code>
<Unit>D></Unit>
<Amount>125.00></Amount>
</ClientAsset>
<ClientAsset>
<Code>jk12></Code>
<Unit>C</Unit>
<Amount>125.00></Amount>
</ClientAsset>
</Assets>
</criteria>
</CreateClient>
Thanks,
Divya
|
|

April 20th, 2010, 09:58 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Those characters are copied from your input sample to the output. If those charactes exist in your real input but you don't want them in the output then you need to remove them with e.g.
Code:
<Code><xsl:value-of select="translate(../*[local-name() = concat('CODE', $pos)], '>', '')"/></Code>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|

April 20th, 2010, 10:06 AM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Martin,
now i am getting the correct output, but my input xml doesn't have those extra characters.
Thanks,
Divya.
|
|

April 20th, 2010, 10:14 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
The sample you posted in Mutiple tags in XSLT does have extra '>' characters in the element contents of your AMOUNTx elements.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|

April 20th, 2010, 11:29 AM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
I am sorry Martin.
I corrected my input xml.
Now it's working fine.
Thanks a lot
|
|
 |