使用XSLT的XML排序问题

时间:2012-10-29 13:10:46

标签: xml xslt sorting

我想对xml进行排序

我的xml格式是

<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>

和我使用的XSL是

<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>

我想使用AdditionalDetail的IntPart属性进行排序。

请有人帮帮我

2 个答案:

答案 0 :(得分:1)

这实际上不是排序问题,而是命名空间问题!在原始XML中,您已指定了默认命名空间

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

这意味着XML中的所有元素都在该命名空间内。但是,在您的XSLT中,您没有引用此命名空间,并且正在寻找根本不在命名空间中的元素。

要解决这个问题,您需要使用您喜欢的任何字母前缀在XSLT中指定命名空间,然后在元素名称前加上前缀,以表明它们需要位于该名称空间中。

试试这个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>

请注意,我还添加了一个模板以匹配 AdditionalDetails ,因为它看起来像是要输出元素的值。

答案 1 :(得分:0)

如果您只想对SummaryResponse元素进行排序,则此样式表有效:

<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>