You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Stephane Dion <st...@accovia.com> on 2005/10/20 15:51:08 UTC

xalan xsl:sort does not sort!

Hi

 

            I'm using Xalan and trying to sort blocks of element but without
success.

 

            I'm trying to sort each block of NetPrice element by the
attribute NetAmount.  In each ProductOption, I have multiple NetPrice
element

 

            Check my example below. 

 

Thanks for your help

Stephane

 

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

  <xsl:output method="xml"/>

 

  <xsl:template
match="/GetProductOptionsResponse/DetailedProduct/ProductOptionGroup/Product
Option">

    <xsl:copy>

       <xsl:apply-templates>

         <xsl:sort data-type="number" select="NetPrice/@netAmount"
order="ascending"/>

       </xsl:apply-templates>

    </xsl:copy>

  </xsl:template>

 

  <xsl:template match="*">

    <xsl:copy>

      <xsl:apply-templates/>

    </xsl:copy>

  </xsl:template>

</xsl:stylesheet>

 

 

<GetProductOptionsResponse xmlns="http://www.org/TRIP2004A/01";> 

    <Header language="en" version="5.1.0"/>

        <DetailedProduct availabilityStatus="available">        

        <ProductOptionGroup description="OPTION GROUP 1">            

            <ProductOption description="option A">

               <NetPrice currency="EUR" netAmount="2.27"
priceReferenceID="HTLAVO13XAAX" quantity="1"/>

               <NetPrice currency="EUR" netAmount="-45.09"
priceReferenceID="HTLAVO13XAAX" quantity="1"/>

               <TravelerPrice age="10" travelerId="003">

                    <PriceSummary baseAmount="0.00"/>

               </TravelerPrice>

            </ProductOption>

            <ProductOption description="option B">

                <NetPrice currency="EUR" netAmount="222.27"
priceReferenceID="HTLAVO13XAAX" quantity="1"/>

                <NetPrice currency="EUR" netAmount="45.09"
priceReferenceID="HTLAVO13XAAX" quantity="1"/>

               <TravelerPrice age="10" travelerId="003">

                    <PriceSummary baseAmount="0.00"/>

               </TravelerPrice>

            </ProductOption>

        </ProductOptionGroup>

        <ProductOptionGroup description="OPTION GROUP 2">            

            <ProductOption description="option Z">

                <NetPrice currency="EUR" netAmount="5.55"
priceReferenceID="BHTLAVO13XAAX" quantity="1"/>

                <NetPrice currency="EUR" netAmount="-145.09"
priceReferenceID="BHTLAVO13XAAX" quantity="1"/>

               <TravelerPrice age="99" travelerId="001">

                    <PriceSummary baseAmount="1.00"/>

               </TravelerPrice>

            </ProductOption>

        </ProductOptionGroup>

        </DetailedProduct>

</GetProductOptionsResponse>

 

 

 


Re: RE : xalan xsl:sort does not sort!

Posted by Henry Zongaro <zo...@ca.ibm.com>.
David Bertoni <db...@apache.org> wrote on 2005-10-21 01:59:50 PM:
> Henry Zongaro wrote:
> > The default value for the select attribute of the xsl:apply-templates 
> > instruction is "*", which means that templates matching the element 
> > children of the current node will be applied.
> 
> Not quite true.  If the xsl:apply-templates instruction does not have a 
> select attribute, then default behavior is to select all of the children 

> of the current node.

Whoops!  Thanks, Dave, for catching my mistake.

My apologies for any confusion I may have caused.

Thanks,

Henry
------------------------------------------------------------------
Henry Zongaro      Xalan development
IBM SWS Toronto Lab   T/L 969-6044;  Phone +1 905 413-6044
mailto:zongaro@ca.ibm.com

Re: RE : xalan xsl:sort does not sort!

Posted by David Bertoni <db...@apache.org>.
Henry Zongaro wrote:
> Hi, Stephane.
> 
> "Stephane Dion" <st...@accovia.com> wrote on 2005-10-20 02:51:26 PM:
> 
>>Like you mention, I would like to have the same input XML as my 
>>output XML (with the netPrice sorted) but the attributes are not 
>>copied even if I have the xsl:copy.  Can you help me on this?  
> 
> 
> The default value for the select attribute of the xsl:apply-templates 
> instruction is "*", which means that templates matching the element 
> children of the current node will be applied.

Not quite true.  If the xsl:apply-templates instruction does not have a 
select attribute, then default behavior is to select all of the children 
of the current node.

http://www.w3.org/TR/xslt#section-Applying-Template-Rules

"In the absence of a select attribute, the xsl:apply-templates 
instruction processes all of the children of the current node, including 
text nodes."

So the real default is "node()".

> 
> See [1] for tutorial information about XSLT.  You can find information 
> about the Mulberry XSL Mailing List at [2], which is a good place to ask 
> questions about XSLT.  The xalan-j-users mailing list is really intended 
> as a place to discuss issues that are specific to the Xalan-J processors.
>

I agree with Henry.  You will not find a better resource for XSLT 
questions than the Mulberry list.

Dave

RE : xalan xsl:sort does not sort!

Posted by Henry Zongaro <zo...@ca.ibm.com>.
Hi, Stephane.

"Stephane Dion" <st...@accovia.com> wrote on 2005-10-20 02:51:26 PM:
> Like you mention, I would like to have the same input XML as my 
> output XML (with the netPrice sorted) but the attributes are not 
> copied even if I have the xsl:copy.  Can you help me on this?  

The default value for the select attribute of the xsl:apply-templates 
instruction is "*", which means that templates matching the element 
children of the current node will be applied.  So your stylesheet copies a 
node and then invokes apply templates for the child elements of that node, 
never doing the same for the attributes.  The path expression for 
selecting all attributes of the context node is "@*".  Your stylesheet 
will similarly need a template that matches any attribute node and copies 
it to the output document.
 
> I tried to add my namespace but it?s not working anymore with it.

The namespace prefix needs to be specified for each name to which it 
applies.

Here's a modified stylesheet that should do what you expect:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
 xmlns:aco="http://www.org/TRIP2004A/01"
version="1.0">
  <xsl:output method="xml"/>
 
  <xsl:template 
match="/aco:GetProductOptionsResponse/aco:DetailedProduct/aco:ProductOptionGroup/aco:ProductOption">
    <xsl:copy>
       <xsl:apply-templates select="@*"/>
       <xsl:apply-templates>
         <xsl:sort data-type="number" select="@netAmount" 
order="ascending"/>
       </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
 
  <xsl:template match="*|@*">
    <xsl:copy>
      <xsl:apply-templates select="*|@*"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

See [1] for tutorial information about XSLT.  You can find information 
about the Mulberry XSL Mailing List at [2], which is a good place to ask 
questions about XSLT.  The xalan-j-users mailing list is really intended 
as a place to discuss issues that are specific to the Xalan-J processors.

I hope that helps.

Thanks,

Henry
[1] http://xml.apache.org/xalan-j/overview.html#uptospeed
[2] http://xml.apache.org/xalan-j/faq.html#faq-N10025
------------------------------------------------------------------
Henry Zongaro      Xalan development
IBM SWS Toronto Lab   T/L 969-6044;  Phone +1 905 413-6044
mailto:zongaro@ca.ibm.com

RE : xalan xsl:sort does not sort!

Posted by Stephane Dion <st...@accovia.com>.
 

It works!  Thanks a lot Henry.

 

Like you mention, I would like to have the same input XML as my output XML
(with the netPrice sorted) but the attributes are not copied even if I have
the xsl:copy.  Can you help me on this?  

 

I tried to add my namespace but it’s not working anymore with it.  I read
many web pages and tried many things.  Here is my new stylesheet with my
namespace, what’s wrong:

 

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 

 xmlns:aco="http://www.org/TRIP2004A/01"

version="1.0">

  <xsl:output method="xml"/>

 

  <xsl:template
match="aco:/GetProductOptionsResponse/DetailedProduct/ProductOptionGroup/Pro
ductOption">

    <xsl:copy>

       <xsl:apply-templates>

         <xsl:sort data-type="number" select="@netAmount"
order="ascending"/>

       </xsl:apply-templates>

    </xsl:copy>

  </xsl:template>

 

  <xsl:template match="*">

    <xsl:copy>

      <xsl:apply-templates/>

    </xsl:copy>

  </xsl:template>

</xsl:stylesheet>

 

 

Thanks a lot again!

Stephane

 

 

-----Message d'origine-----
De : Henry Zongaro [mailto:zongaro@ca.ibm.com] 
Envoyé : October 20, 2005 13:37
À : stephane@accovia.com
Cc : xalan-j-users@xml.apache.org
Objet : Re: xalan xsl:sort does not sort!

 

Hi, Stephane.

 

"Stephane Dion" <st...@accovia.com> wrote on 2005-10-20 09:51:08 AM:

> I?m trying to sort each block of NetPrice element by the attribute 

> NetAmount.  In each ProductOption, I have multiple NetPrice element

> 

> Check my example below. 

 

Focusing on the first template in your sample:

 

>   <xsl:template 

> 

match="/GetProductOptionsResponse/DetailedProduct/ProductOptionGroup/Product
Option">

>     <xsl:copy>

>        <xsl:apply-templates>

>          <xsl:sort data-type="number"

>                    select="NetPrice/@netAmount" 

>                    order="ascending"/>

>        </xsl:apply-templates>

>     </xsl:copy>

>   </xsl:template>

 

Each node selected for the xsl:apply-templates instruction, is used as the 

context node to evaluate the select expression in the xsl:sort.  With your 

input document, the xsl:apply-templates selects the NetPrice children of 

ProductOption.  Each NetPrice node becomes the context node for evaluating 

the expression NetPrice/@netAmount in the xsl:sort, which results in an 

empty node set, because no NetPrice element has a NetPrice element as a 

child.

 

If you change the xsl:sort to the following, it should work:

 

 

         <xsl:sort data-type="number"

                   select="@netAmount" 

                   order="ascending"/>

 

I hope that helps.

 

As an aside, I noticed that the elements in your input document have a 

non-null namespace, but you didn't use that in the match pattern for the 

first template.  I also noticed that you're not copying any attributes. 

I'm guessing that both of those problems arose when you produced your 

example stylesheet, and the original stylesheet handles those things 

correctly, but I thought I'd point them out anyway just in case.

 

Thanks,

 

Henry

------------------------------------------------------------------

Henry Zongaro      Xalan development

IBM SWS Toronto Lab   T/L 969-6044;  Phone +1 905 413-6044

mailto:zongaro@ca.ibm.com

 

 


Re: xalan xsl:sort does not sort!

Posted by Henry Zongaro <zo...@ca.ibm.com>.
Hi, Stephane.

"Stephane Dion" <st...@accovia.com> wrote on 2005-10-20 09:51:08 AM:
> I?m trying to sort each block of NetPrice element by the attribute 
> NetAmount.  In each ProductOption, I have multiple NetPrice element
> 
> Check my example below. 

Focusing on the first template in your sample:

>   <xsl:template 
> 
match="/GetProductOptionsResponse/DetailedProduct/ProductOptionGroup/ProductOption">
>     <xsl:copy>
>        <xsl:apply-templates>
>          <xsl:sort data-type="number"
>                    select="NetPrice/@netAmount" 
>                    order="ascending"/>
>        </xsl:apply-templates>
>     </xsl:copy>
>   </xsl:template>

Each node selected for the xsl:apply-templates instruction, is used as the 
context node to evaluate the select expression in the xsl:sort.  With your 
input document, the xsl:apply-templates selects the NetPrice children of 
ProductOption.  Each NetPrice node becomes the context node for evaluating 
the expression NetPrice/@netAmount in the xsl:sort, which results in an 
empty node set, because no NetPrice element has a NetPrice element as a 
child.

If you change the xsl:sort to the following, it should work:


         <xsl:sort data-type="number"
                   select="@netAmount" 
                   order="ascending"/>

I hope that helps.

As an aside, I noticed that the elements in your input document have a 
non-null namespace, but you didn't use that in the match pattern for the 
first template.  I also noticed that you're not copying any attributes. 
I'm guessing that both of those problems arose when you produced your 
example stylesheet, and the original stylesheet handles those things 
correctly, but I thought I'd point them out anyway just in case.

Thanks,

Henry
------------------------------------------------------------------
Henry Zongaro      Xalan development
IBM SWS Toronto Lab   T/L 969-6044;  Phone +1 905 413-6044
mailto:zongaro@ca.ibm.com