p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > XML > XSLT
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 25th, 2008, 01:43 PM
Authorized User
Points: 68, Level: 1
Points: 68, Level: 1 Points: 68, Level: 1 Points: 68, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2008
Location: , , .
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old November 25th, 2008, 01:53 PM
samjudson's Avatar
Friend of Wrox
Points: 4,453, Level: 28
Points: 4,453, Level: 28 Points: 4,453, Level: 28 Points: 4,453, Level: 28
Activity: 61%
Activity: 61% Activity: 61% Activity: 61%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 1,359
Thanks: 0
Thanked 31 Times in 31 Posts
Default

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 -/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old November 25th, 2008, 01:59 PM
Authorized User
Points: 68, Level: 1
Points: 68, Level: 1 Points: 68, Level: 1 Points: 68, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2008
Location: , , .
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default

It works! You Rock!

Have a nice holiday!

George Meng
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old November 25th, 2008, 02:08 PM
mhkay's Avatar
Wrox Author
Points: 12,735, Level: 48
Points: 12,735, Level: 48 Points: 12,735, Level: 48 Points: 12,735, Level: 48
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 Posts
Default

>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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old December 1st, 2008, 12:13 PM
Authorized User
Points: 68, Level: 1
Points: 68, Level: 1 Points: 68, Level: 1 Points: 68, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2008
Location: , , .
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting some nodes by attribute value acw274 XSLT 8 July 2nd, 2008 02:22 AM
Adding nodes to JTree sriharshareddyk Java Basics 0 May 4th, 2007 02:18 PM
adding prefix to nodes and subnodes to output xml raghurns XSLT 9 November 17th, 2006 04:41 PM
Adding nodes to grandparent of current spencer.clark XSLT 2 August 12th, 2005 10:29 AM
HELP! More complicate sorting nodes question kevin_in_black XSLT 2 April 13th, 2004 06:51 AM



All times are GMT -4. The time now is 05:45 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc