Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old April 20th, 2010, 09:10 AM
Authorized User
 
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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
 
Old April 20th, 2010, 09:23 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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
 
Old April 20th, 2010, 09:48 AM
Authorized User
 
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Martin,
Thanks for the response.
I am getting extra characters i.e &gt;
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&gt;</Code>
<Unit>A&gt;</Unit>
<Amount>25.00&gt;</Amount>
</ClientAsset>
<ClientAsset>
<Code>CB12&gt;</Code>
<Unit>D&gt;</Unit>
<Amount>125.00&gt;</Amount>
</ClientAsset>
<ClientAsset>
<Code>jk12&gt;</Code>
<Unit>C</Unit>
<Amount>125.00&gt;</Amount>
</ClientAsset>
</Assets>
</criteria>
</CreateClient>
Thanks,
Divya
 
Old April 20th, 2010, 09:58 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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)], '&gt;', '')"/></Code>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old April 20th, 2010, 10:06 AM
Authorized User
 
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Martin,

now i am getting the correct output, but my input xml doesn't have those extra characters.

Thanks,
Divya.
 
Old April 20th, 2010, 10:14 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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
 
Old April 20th, 2010, 11:29 AM
Authorized User
 
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I am sorry Martin.
I corrected my input xml.
Now it's working fine.
Thanks a lot





Similar Threads
Thread Thread Starter Forum Replies Last Post
strip html tags with xslt smiter XSLT 4 June 19th, 2012 08:34 AM
xslt for tags dnandhak XSLT 4 May 23rd, 2008 05:57 AM
add HTML tags via XSLT chopswil2 XSLT 3 November 5th, 2007 09:30 AM
Replacing tags within xml using xslt patelgaurav85 XSLT 0 June 19th, 2007 12:34 PM
XSLT output with tags trinkets XSLT 22 November 14th, 2006 11:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.