Wrox Programmer Forums
|
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 July 10th, 2007, 12:53 PM
Authorized User
 
Join Date: Jul 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default sorting

Hi,

Can anyone please help me?

I have the xml file:

<Issue>
     <IssueHistory_CreationDate>2004-01-01</IssueHistory_CreationDate>
      <Status>
           <Name>Find</Name>
                <Amount>28</Amount>
           <Name>resolved</Name>
                <Amount>0</Amount>
            <Name>Finished</Name>
                <Amount>0</Amount>
   </Status>
    <IssueHistory_CreationDate>2004-01-02</IssueHistory_CreationDate>
    <Status>
           <Name>Find</Name>
               <Amount>6</Amount>
          <Name>Resolved</Name>
                <Amount>6</Amount>
           <Name>Finished</Name>
                <Amount>4</Amount>
   </Status>

But I want to sort the Fined, Resolved and Finished amount
with xslt.

I want that the xslt file generate my xml file to this:

<string>Fined</string>
<Number>28</Number>
<Number>6</Number>

<string>solved</string>
<Number>0</Number>
<Number>6</Number>

<string>Finished</string>
<Number>0</Number>
<Number>4</Number>

Does anyone know how I can do this?

 
Old July 10th, 2007, 01:21 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If the set of status values is fixed, just do

<xsl:template name="statusReport">
  <xsl:param name="status"/>
  <string><xsl:value-of select="$status"/></string>
  <xsl:for-each select="IssueHistory/Status[Name=$status]">
    <Number><xsl:value-of select="Amount"/></Number>
  </
</

<xsl:template match="Issue">
  <xsl:call-template name="statusReport">
    <xsl:with-param name="status">Find</xsl:with-param>
  </xsl:call-template>
  <xsl:call-template name="statusReport">
    <xsl:with-param name="status">Resolved</xsl:with-param>
  </xsl:call-template>

etc.

If the set of status values isn't fixed, then you have a grouping problem (or you could tackle it as a grouping problem anyway) - use xsl:for-each-group in XSLT 2.0, or see www.jenitennison.com/xslt/grouping

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 10th, 2007, 01:48 PM
Authorized User
 
Join Date: Jul 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Mhkay,

what do mean with If the set of status values isn't fixed?

I want to show al the 3, Fined, Solved and Finished. The file must show the Fined, Solved and Finished with grouping their amount.



 
Old July 10th, 2007, 02:51 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If your input documents always use "Fined" "Solved" and "Finished" then that's what I call a fixed set of values. If different documents might use different values, for example "Complete" and "Postponed" then you need to use grouping techniques.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 10th, 2007, 03:17 PM
Authorized User
 
Join Date: Jul 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi again,

okee, thanks. But wich of the two should I use?

<xsl:template name="statusReport">
  <xsl:param name="status"/>
  <string><xsl:value-of select="$status"/></string>
  <xsl:for-each select="IssueHistory/Status[Name=$status]">
    <Number><xsl:value-of select="Amount"/></Number>
  </
</

<xsl:template match="Issue">
  <xsl:call-template name="statusReport">
    <xsl:with-param name="status">Find</xsl:with-param>
  </xsl:call-template>
  <xsl:call-template name="statusReport">
    <xsl:with-param name="status">Resolved</xsl:with-param>
  </xsl:call-template>


 
Old July 10th, 2007, 03:23 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Eh?

I gave you two templates one of which calls the other. If you can't work out how to integrate those into a stylesheet, then you need to do some more basic reading before you do any more coding.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 10th, 2007, 03:49 PM
Authorized User
 
Join Date: Jul 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi again,

Yes, I am busy with basic reading.

first of all thank you for your help.

I have this:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>

<xsl:template name="statusReport">
<xsl:param name="status"/>
<string><xsl:value-of select="$status"/></string>
<xsl:for-each select="IssueHistory/Status[Name=$status]">
<Number><xsl:value-of select="Amount"/></Number>

 <row >
        <xsl:for-each select="Issue/IssueHistory_CreationDate">
         <String><xsl:apply-templates/></String>
        </xsl:for-each>
   </row>

<xsl:template match="Issue">
<xsl:call-template name="statusReport">
<xsl:with-param name="status">Find</xsl:with-param>
</xsl:call-template>

<xsl:call-template name="statusReport">
<xsl:with-param name="status">Resolved</xsl:with-param>
</xsl:call-template>

</xsl:template>
</xsl:stylesheet>

Is this the correct xslt?

 
Old July 10th, 2007, 04:04 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You need to add the missing end tags to make it well-formed. I used </ as a short-hand because that's what I use in an XML editor, and I assume people can fill in the gaps.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 11th, 2007, 02:52 AM
Authorized User
 
Join Date: Jul 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>

<xsl:template name="statusReport">
<xsl:param name="status"/><string><xsl:value-of select="$status"/></string>

<xsl:for-each select="Issue/Status[Name=$status]"><Number><xsl:value-of select="Amount"/></Number>
</xsl:for-each>
<xsl:template match="Issue"></xsl:template>

<Chart_data>

 <row >
        <xsl:for-each select="Issue/IssueHistory_CreationDate">
         <String><xsl:apply-templates/></String>
        </xsl:for-each>
   </row>

<row>
<xsl:call-template name="statusReport">
<xsl:with-param name="status">Find</xsl:with-param>
</xsl:call-template>
</row>

<row>
<xsl:call-template name="statusReport">
<xsl:with-param name="status">Resolved</xsl:with-param>
</xsl:call-template>
</row>

</Chart_data>
</xsl:template>
</xsl:stylesheet>

When I translate this withe the xml file i got a error. It says:
Invalid at the top level of the document. Error processing resource 'file:///C:/Documents and Settings/Desktop/resu...
 
Old July 11th, 2007, 05:46 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Try closing the first template before you open the second one.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
SORTING pallone XSLT 3 October 29th, 2006 08:45 AM
Sorting sunny76 Excel VBA 2 September 19th, 2005 09:31 PM
Datagrid sorting by non alphabetical sorting? LLAndy VS.NET 2002/2003 1 July 15th, 2004 01:20 AM
Sorting? pbernardo XSLT 2 October 27th, 2003 11:34 AM
Need help Sorting athanatos XSLT 5 August 11th, 2003 07:51 PM





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