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 November 25th, 2008, 01:43 PM
Authorized User
 
Join Date: Jun 2008
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
 
Old November 25th, 2008, 01:53 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 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 -/
 
Old November 25th, 2008, 01:59 PM
Authorized User
 
Join Date: Jun 2008
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default

It works! You Rock!

Have a nice holiday!

George Meng
 
Old November 25th, 2008, 02:08 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
 
Old December 1st, 2008, 12:13 PM
Authorized User
 
Join Date: Jun 2008
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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting some nodes by attribute value acw274 XSLT 8 July 2nd, 2008 01:22 AM
Adding nodes to JTree sriharshareddyk Java Basics 0 May 4th, 2007 01: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 09:29 AM
HELP! More complicate sorting nodes question kevin_in_black XSLT 2 April 13th, 2004 05:51 AM





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