0

I want to sort a xml

my xml format is

<OTA_HotelAvailRS xmlns="http://www.opentravel.org/OTA" EchoToken="" SequenceNmbr="1" Target="Production" TimeStamp="2012-09-28T08:29:44Z" Version="1">
    <SummaryResponse>    
        <AdditionalDetails>    
           <AdditionalDetail Amount="212,20 EUR" Code="TotalPackagePrice" CurrencyCode="EUR" DecPart="20" IntPart="212" Type="PackageInformation"></AdditionalDetail>
          </AdditionalDetails>

    </SummaryResponse>
    <SummaryResponse>

       <AdditionalDetails>
          <AdditionalDetail Amount="212,20 EUR" Code="TotalPackagePrice" CurrencyCode="EUR" DecPart="20" IntPart="500" Type="PackageInformation"></AdditionalDetail>
        </AdditionalDetails>
    </SummaryResponse>
</OTA_HotelAvailRS>

and XSL which I m using is

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

<xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

<xsl:template match="OTA_HotelAvailRS">
  <xsl:copy>   
    <xsl:apply-templates select="SummaryResponse/AdditionalDetails">
       <xsl:sort select="AdditionalDetail/@IntPart" data-type="number" order="descending"/>
    </xsl:apply-templates>
    Value of :<xsl:value-of select="AdditionalDetail/@IntPart" />
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

I want to sort with the IntPart attribute of the AdditionalDetail.

Please some one help me out

2
  • Can you give an example of the output you require? Commented Oct 29, 2012 at 13:24
  • Actually I want the Whole XMl but only sorted to the Child Attribute values... Commented Oct 31, 2012 at 7:41

2 Answers 2

2

This is not actually a sorting issue, but a namespace issue! In your original XML, you have specified a default namespace

<OTA_HotelAvailRS xmlns="http://www.opentravel.org/OTA" 

This means all the elements in your XML are within that namespace. However, in your XSLT you make no reference to this namespace, and are looking for elements that are not in a namespace at all.

To get around this, you will need to specify the namespace in your XSLT, using any letter prefix you like, and then prefix your element names to indicate they need to be in that name space.

Try this XSLT

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

   <xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes"/>

   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="a:OTA_HotelAvailRS">
      <xsl:copy>
         <xsl:apply-templates select="a:SummaryResponse/a:AdditionalDetails">
            <xsl:sort select="a:AdditionalDetail/@IntPart" data-type="number" order="descending"/>
         </xsl:apply-templates>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="a:AdditionalDetails">
     Value of : <xsl:value-of select="a:AdditionalDetail/@IntPart"/>
   </xsl:template>
</xsl:stylesheet>

Note that I have also added a template to match AdditionalDetails as it looked like you wanted to output the value for the element.

Sign up to request clarification or add additional context in comments.

Comments

1

If you just want the SummaryResponse elements sorted, this stylesheet works:

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ota="http://www.opentravel.org/OTA">

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="ota:OTA_HotelAvailRS">
    <xsl:copy>
      <xsl:apply-templates select="ota:SummaryResponse">
        <xsl:sort select="ota:AdditionalDetails/ota:AdditionalDetail/@IntPart" data-type="number" order="descending"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.