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 9th, 2010, 08:54 AM
Authorized User
 
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default Increment tag by 1 using XSLT

Hello frnds,

Input xml:
<LMPRequests>V1.00
<LMPRequest>
<LMPMessage>Detailsr<Section>
Criteria
<Row>
<autoCorrList>
<typed>1</typed>
<color>red</color>
</autoCorrList>
<autoCorrList>
<typed>2</typed>
<color>blue</color>
</autoCorrList>
</Row>
</Section>
</LMPMessage>
</LMPRequest>
</LMPRequests>

XSLT :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:template match="LMPRequests">
<xsl:apply-templates select="LMPRequest"></xsl:apply-templates>
</xsl:template>
<xsl:template match="LMPRequest">
</xsl:template>
<xsl:template match="autoCorrList">
<xsl:element name="autoCorrList{count(preceding-sibling::autoCorrList)}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

Output xml should be like :

<autoCorrList1>
<typed>1</typed>
<color>red</color>
<autoCorrList1>
<autoCorrList2>
<typed>2<typed>
<color>blue<color>
<autoCorrList2>

with the xslt i wrote i am getting only
<?xml version="1.0" encoding="UTF-8"?> :)

Can any one please correct my XSLT to get the expected output xml.

Thanks,
Divya
 
Old April 9th, 2010, 09:01 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Code:
<xsl:template match="LMPRequest"> 
</xsl:template>
This template tells the XSLT processor to stop processing the XML when it gets to a LMPRequest element and not processes any children elements. So it does.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old April 9th, 2010, 09:24 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Your output xml does not have a root element. Try the below code with <root> as root element:
Code:
<xsl:template match="LMPRequests">
<root>
<xsl:apply-templates select="//autoCorrList"></xsl:apply-templates>
</root>
</xsl:template> 
<xsl:template match="autoCorrList">
<xsl:copy-of select="."/>
</xsl:template>
__________________
Rummy
 
Old April 9th, 2010, 09:45 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Sorry, I gave an incorrect solution above. Please try the below:
Code:
 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="LMPRequests">
<root>
<xsl:for-each select="//autoCorrList">
<xsl:variable name="posi" select="position()"/>
<xsl:variable name="name" select="local-name()"/>
<xsl:apply-templates select=".">
<xsl:with-param name="posi" select="$posi"/>
<xsl:with-param name="name" select="$name"/>
</xsl:apply-templates>
</xsl:for-each>
</root>
</xsl:template> 
 
<xsl:template match="autoCorrList">
<xsl:param name="posi"/>
<xsl:param name="name"/>
<xsl:element name="{concat($name, $posi)}">
<xsl:copy-of select="*"/>
</xsl:element>
</xsl:template>
There can be better solution than the one above.
__________________
Rummy

Last edited by mrame; April 9th, 2010 at 09:51 AM.. Reason: Inserted xsl:element
 
Old April 9th, 2010, 09:53 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

No, Bad developer. :)

Your first try was so much closer. When you start to write XML element by trying to use disable-output-escaping you know you are doing it wrong.

Code:
<xsl:template match="LMPRequests">
	<root><xsl:apply-templates select="//autoCorrList"/></root>
</xsl:template>	

<xsl:template match="autoCorrList">
<xsl:element name="autoCorrList{position()}">
<xsl:copy-of select="*"/>
</xsl:element>
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old April 12th, 2010, 09:00 AM
Authorized User
 
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Sam,

I should also match LMPRequest , else i am getting an error "Pretty-print is not possible because the file is not well formed". with the XSLT you suggest, we didn't mention LMPRequest. How to nest two templates. In my XML i have more than 20 fields to map to the output xml other than autoCorrList.

please help me in nesting two tempaltes.

Please check the below XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:template match="LMPRequests">
<xsl:apply-templates select="LMPRequest"/>
<xsl:apply-templates select="LMPRequest">
</xsl:apply-templates>
</xsl:template>

<xsl:template match="LMPRequest">
<AddInvestor xmlns="http://byuus.com/ufus/webservices">
<criteria>
<address1>
<xsl:value-of select="LMPMessage/Section/Row/ADDRESS1"/>
</address1>
</criteria>
</AddInvestor >
</xsl:template>
</xsl:stylesheet>

Now my query is how do i nest my condition i.e
<xsl:template match="autoCorrList">
<xsl:element name="autoCorrList{position()}">
<xsl:copy-of select="*"/>
</xsl:element>
</xsl:template>

Thanks,
divya
 
Old April 12th, 2010, 09:33 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Your need is unclear. In the previous input xml you posted, there was no ADDRESS1 element. But now you are matching the same. So post the input xml, and the output xml yo require.
__________________
Rummy
 
Old April 12th, 2010, 10:58 AM
Authorized User
 
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Rummy,
sorry for the confusion.
Input xml :
<LMPRequests>V1.00
<LMPRequest>
<LMPMessage>Investordetails
<Section>Criteria
<Row>
<address1 >Nawabpet</address1>
<autoCorrList>
<typed>1</typed>
<color>red</color>
</autoCorrList>
<autoCorrList>
<typed>2</typed>
<color>blue</color>
</autoCorrList>
</Row>
</Section>
</LMPMessage>
</LMPRequest>
</LMPRequests>



My output xml should be :


<?xml version="1.0" encoding="UTF-8"?>
<Investordetails xmlns="http://byuus.com/ufs1/webservices">
<criteria>
<address1>Nawabpet</address1>
<autoCorrList1>
<typed>1</typed>
<color>red</color>
</autoCorrList1>
<autoCorrList2>
<typed>2</typed>
<color>blue</color>
</autoCorrList2>

</criteria>
</Investordetails >


thanks,
divya
 
Old April 13th, 2010, 01:22 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try the below:
Code:
<xsl:template match="LMPRequests">
<xsl:apply-templates select="LMPRequest"/>
</xsl:template>
 
<xsl:template match="LMPRequest">
<Investordetails xmlns="http://byuus.com/ufs1/webservices">
<criteria>
<address1>
<xsl:value-of select="LMPMessage/Section/Row/address1"/>
</address1>
<xsl:for-each select="//autoCorrList">
<xsl:element name="autoCorrList{position()}">
<xsl:copy-of select="*"/>
</xsl:element>
</xsl:for-each>
</criteria>
</Investordetails>
</xsl:template>
__________________
Rummy
 
Old April 13th, 2010, 05:03 AM
Authorized User
 
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Rummy,

Thank u so much.
I am getting the expected output.
Can u suggest me an url to learn XSLT and good practice of XSLT.

Thanks,
divya





Similar Threads
Thread Thread Starter Forum Replies Last Post
An XML value in an Html tag (xslt) ismailc XSLT 9 October 19th, 2009 07:27 AM
Increment a tag stefan.yu XSLT 10 April 16th, 2007 05:45 AM
xslt for each and attribute tag desse XSLT 1 May 24th, 2006 08:01 AM
how to get xml tag names in xslt mdawoodk XSLT 8 March 14th, 2005 05:55 AM
XSLT incorrectly closing my div tag Blaise XML 2 June 2nd, 2004 07:56 AM





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