0

我有一个这样的输入 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,我需要对距离上的位置节点进行排序,然后按最小距离对学校进行排序基本上输出应该是这样的

<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 上再次对其进行排序来做到这一点。

我想知道是否有一种方法可以一次性完成所有这些。

感谢你的帮助。

4

1 回答 1

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,我认为您对变量进行排序的方法是正确的。

于 2013-06-16T09:53:05.217 回答