两个节点集的XSL排序

时间:2013-06-15 12:48:46

标签: sorting xslt

我有这样的输入XML: -

<Results>
<School>
    <Name>A</Name>
    <Location>
        <StreetAddress>abc</StreetAddress>
        <Distance>5</Distance>
    </Location>
    <Location>
        <StreetAddress>def</StreetAddress>
        <Distance>3</Distance>
    </Location>
    <Location>
        <StreetAddress>xyz</StreetAddress>
        <Distance>6</Distance>
    </Location>
</School>
<School>
    <Name>B</Name>
    <Location>
        <StreetAddress>abc1</StreetAddress>
        <Distance>7</Distance>
    </Location>
    <Location>
        <StreetAddress>def2</StreetAddress>
        <Distance>6</Distance>
    </Location>
    <Location>
        <StreetAddress>xyz3</StreetAddress>
        <Distance>2</Distance>
    </Location>
</School>
<School>
    <Name>C</Name>
    <Location>
        <StreetAddress>abc4</StreetAddress>
        <Distance>2</Distance>
    </Location>
    <Location>
        <StreetAddress>def5</StreetAddress>
        <Distance>1</Distance>
    </Location>
    <Location>
        <StreetAddress>xyz6</StreetAddress>
        <Distance>0.5</Distance>
    </Location>
</School>   

使用XSL v1,我需要对Distance上的Location节点进行排序,然后按最小距离对学校进行排序 基本上输出应该看起来像这样

<Results>
<School>
    <Name>C</Name>
    <Location>
        <StreetAddress>xyz6</StreetAddress>
        <Distance>0.5</Distance>
    </Location>
    <Location>
        <StreetAddress>def5</StreetAddress>
        <Distance>1</Distance>
    </Location>
    <Location>
        <StreetAddress>abc4</StreetAddress>
        <Distance>2</Distance>
    </Location>
</School>   
<School>
    <Name>B</Name>
    <Location>
        <StreetAddress>xyz3</StreetAddress>
        <Distance>2</Distance>
    </Location>
    <Location>
        <StreetAddress>def2</StreetAddress>
        <Distance>6</Distance>
    </Location>
    <Location>
        <StreetAddress>abc1</StreetAddress>
        <Distance>7</Distance>
    </Location>
</School>
<School>
    <Name>A</Name>
    <Location>
        <StreetAddress>def</StreetAddress>
        <Distance>3</Distance>
    </Location>
    <Location>
        <StreetAddress>abc</StreetAddress>
        <Distance>5</Distance>
    </Location>
    <Location>
        <StreetAddress>xyz</StreetAddress>
        <Distance>6</Distance>
    </Location>
</School>

我能够通过首先根据距离对节点进行排序,将它们存储在变量中,然后在/ Results / School / Location [1] / Distance上再次对其进行排序。

我想知道是否有办法一次性执行所有这些。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

使用XSLT 2.0,您可以轻松完成

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

<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="Results">
  <xsl:copy>
    <xsl:apply-templates select="School">
      <xsl:sort select="min(Location/Distance/number(.))"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

<xsl:template match="School">
  <xsl:copy>
    <xsl:apply-templates select="Name"/>
    <xsl:apply-templates select="Location">
      <xsl:sort select="number(Distance)"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

使用XSLT 1.0,我认为排序到变量的方法是正确的。