|
|
 |
| 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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

November 25th, 2008, 01:43 PM
|
|
Authorized User
|
|
Join Date: Jun 2008
Location: , , .
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Adding and Sorting nodes
Hi guys,
Got a question and after working on it for a couple hours, I think it is time for asking help.
I have an xml file like this:
<?xml version="1.0"?>
<result>
<variable CY="2008"/>
<chartdata>
<row data01="MyCompany" data02="Feb 2008" data03="0.000000"/>
<row data01="MyCompany" data02="May 2008" data03="0.000000"/>
</chartdata>
</result>
My want to convert it to:
<?xml version="1.0" encoding="UTF-8"?>
<result>
<variable CY="2008"/>
<chartdata>
<row data01="MyCompany" data02="Jan 2008" data03="0"/>
<row data01="MyCompany" data02="Feb 2008" data03="0.000000"/>
<row data01="MyCompany" data02="Mar 2008" data03="0" />
<row data01="MyCompany" data02="Apr 2008" data03="0"/>
<row data01="MyCompany" data02="May 2008" data03="0.000000"/>
<row data01="MyCompany" data02="Jun 2008" data03="0"/>
</chartdata>
</result>
Which you can see that I need do add nodes in the right position: order by month name.
Right now, I am able to go this far:
<?xml version="1.0" encoding="UTF-8"?>
<result>
<variable CY="2008"/>
<chartdata>
<row data01="MyCompany" data02="Feb 2008" data03="0.000000"/>
<row data01="MyCompany" data02="May 2008" data03="0.000000"/>
<row data01="MyCompany" data02="Jan 2008" data03="0"/>
<row data01="MyCompany" data02="Mar 2008" data03="0" />
<row data01="MyCompany" data02="Apr 2008" data03="0"/>
<row data01="MyCompany" data02="Jun 2008" data03="0"/>
</chartdata>
</result>
By using this xslt:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="chartdata">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:if test="count(/result/chartdata/row[@data02 = 'Jan 2008' ]) = 0">
<row data01="MyCompany" data02="Jan 2008" data03="0"/>
</xsl:if>
<xsl:if test="count(/result/chartdata/row[@data02 = 'Feb 2008' ]) = 0">
<row data01="MyCompany" data02="Feb 2008" data03="0"/>
</xsl:if>
<xsl:if test="count(/result/chartdata/row[@data02 = 'Mar 2008' ]) = 0">
<row data01="MyCompany" data02="Mar 2008" data03="0"/>
</xsl:if>
<xsl:if test="count(/result/chartdata/row[@data02 = 'Apr 2008' ]) = 0">
<row data01="MyCompany" data02="Apr 2008" data03="0"/>
</xsl:if>
<xsl:if test="count(/result/chartdata/row[@data02 = 'May 2008' ]) = 0">
<row data01="MyCompany" data02="May 2008" data03="0"/>
</xsl:if>
<xsl:if test="count(/result/chartdata/row[@data02 = 'Jun 2008' ]) = 0">
<row data01="MyCompany" data02="Jun 2008" data03="0"/>
</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I can add nodes, but have issue to add & sort nodes at the same time.
Any help are great appreciated!
Best Regards!
George Meng
__________________
George Meng
|

November 25th, 2008, 01:53 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 1,359
Thanks: 0
Thanked 31 Times in 31 Posts
|
|
try something like this:
<xsl:template match="chartdata">
<xsl:call-template name="printData"><xsl:with-param name="date" select="'Jan 2008'"/></xsl:call-template>
<xsl:call-template name="printData"><xsl:with-param name="date" select="'Feb 2008'"/></xsl:call-template>
<xsl:call-template name="printData"><xsl:with-param name="date" select="'Mar 2008'"/></xsl:call-template>
<xsl:call-template name="printData"><xsl:with-param name="date" select="'Apr 2008'"/></xsl:call-template>
<xsl:call-template name="printData"><xsl:with-param name="date" select="'May 2008'"/></xsl:call-template>
<xsl:call-template name="printData"><xsl:with-param name="date" select="'Jun 2008'"/></xsl:call-template>
</xsl:template>
<xsl:template name="printData">
<xsl:param name="date"/>
<xsl:choose>
<xsl:when test="count(/result/chartdata/row[@data02 = $date ]) = 0">
<row data01="MyCompany" data02="{$date}" data03="0"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="/result/chartdata/row[@data02 = $date]" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
There are probably better ways of doing this in XSLT 2.0.
/- Sam Judson : Wrox Technical Editor -/
|

November 25th, 2008, 01:59 PM
|
|
Authorized User
|
|
Join Date: Jun 2008
Location: , , .
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
It works! You Rock!
Have a nice holiday!
George Meng
|

November 25th, 2008, 02:08 PM
|
 |
Wrox Author
Points: 12,735, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 Posts
|
|
>There are probably better ways of doing this in XSLT 2.0.
Not really. You could make it more concise using function calls rather than call-template, and using XPath rather than XSLT conditionals, but the logic would be much the same. If it were a span of 200 months rather than 6 then I would write some kind of loop to generate the date values using format-date(), but with only 6, writing them by hand is easier.
(Though hand-editing the XML is easier still. I wonder what the real requirement is?)
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
|

December 1st, 2008, 12:13 PM
|
|
Authorized User
|
|
Join Date: Jun 2008
Location: , , .
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Hi Michael,
The real requirement is almost the same as my question except:
1) It is 13 months instead of 6 months.
2) The xml data has more columns.
The answer I got from Sam works great in real case.
So thank you all!
George Meng
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |