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

April 9th, 2010, 09:01 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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.
|
|

April 9th, 2010, 09:24 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
|
|
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
|
|

April 9th, 2010, 09:45 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
|
|
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
|
|

April 9th, 2010, 09:53 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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>
|
|

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

April 12th, 2010, 09:33 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
|
|
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
|
|

April 12th, 2010, 10:58 AM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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
|
|

April 13th, 2010, 01:22 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
|
|
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
|
|

April 13th, 2010, 05:03 AM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |