XSLT 按属性值排序 [英] XSLT sort by attribute value

查看:30
本文介绍了XSLT 按属性值排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a question about how to sort based on attribute value.

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

I have the following source document and I would like to sort the track items by the value of the title class value.

希望有人能帮忙解决这个问题.

Hopefully someone can help with this.

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

最终的输出应该是这样的:

The final output should look like this:

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

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

I have tried the following but it does not work.

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

谢谢.

推荐答案

您可以这样做:

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

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

When run on your sample input, the result is:

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

这篇关于XSLT 按属性值排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆