8

我有一个关于如何根据属性值排序的问题。

我有以下源文档,我想按标题类值的值对轨道项目进行排序。

希望有人可以帮助解决这个问题。

 <trackList>

    <track>
        <location>http://localhost/vmydoc</location>
        <title class="STD">Data Two</title>
        </track>
    <track>
        <location>http://localhost/vmydoc</location>
        <title class="SH">Data Three</title>

    </track>
    <track>
        <location>http://localhost/vmydoc</location>
        <title class="STD">Data Four</title>

    </track>
    <track>
        <location>http://localhost/vmydoc</location>
        <title class="SH">Data Five</title>

    </track>
</trackList>

最终输出应如下所示:

<trackList>

    <track>
        <location>http://localhost/vmydoc</location>
        <title class="SH">Data Three</title>

    </track>

    <track>
        <location>http://localhost/vmydoc</location>
        <title class="SH">Data Five</title>

    </track>

    <track>
        <location>http://localhost/vmydoc</location>
        <title class="STD">Data Four</title>

    </track>
    <track>
        <location>http://localhost/vmydoc</location>
        <title class="STD">Data Two</title>
    </track> 
</trackList>

我尝试了以下方法,但它不起作用。

<xsl:for-each-group select="title" group-by="@class">

    <xsl:for-each select="current-group()">
        <xsl:value-of select="@class" />
    </xsl:for-each>

</xsl:for-each-group>

谢谢。

4

1 回答 1

16

您可以按如下方式执行此操作:

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

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

  <xsl:template match="trackList">
    <xsl:copy>
      <xsl:apply-templates select="track">
        <xsl:sort select="title/@class"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

在您的示例输入上运行时,结果是:

<trackList>
  <track>
    <location>http://localhost/vmydoc</location>
    <title class="SH">Data Three</title>

  </track>
  <track>
    <location>http://localhost/vmydoc</location>
    <title class="SH">Data Five</title>

  </track>
  <track>
    <location>http://localhost/vmydoc</location>
    <title class="STD">Data Two</title>
  </track>
  <track>
    <location>http://localhost/vmydoc</location>
    <title class="STD">Data Four</title>

  </track>
</trackList>
于 2013-07-18T03:32:52.030 回答