XSLT按名称对节点进行排序? [英] XSLT to sort nodes by name?

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

问题描述

我不确定xsl:sort指令的工作方式.我需要按标签名称对元素进行排序(以进行比较),但似乎无法提出如何使之工作的方法.尽管我的第一个操作是修改身份转换,然后对其进行修改以包含一个sort语句,但是我不确定如何做到这一点.

I'm unsure how the xsl:sort directive works. I need to sort elements by their tag name (for diffing), and I can't seem to come up with how to make this work. My first though was to modify the identity transform and just modify it to include a sort statement, but I'm not exactly sure how to do that.

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

推荐答案

此转换:

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

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

   <xsl:apply-templates select="node()">
    <xsl:sort select="name()"/>
   </xsl:apply-templates>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

应用于此XML文档:

<t b="x" c="y" a="t">
  <c/>
  <b/>
  <a/>
</t>

产生所需的排序输出:

<t a="t" b="x" c="y">
    <a></a>
    <b></b>
    <c></c>
</t>

记录:

  1. 不仅对元素进行排序,而且对属性进行排序(后者与实现有关,但可以与MSXML一起使用).

  1. Not only the elements but also the attributes are sorted (the latter is implementation dependent, but works OK with MSXML).

将已排序的XML用于差异比较不可靠,因为将XML文档转换为已排序的表示形式不是1:1映射.

Using sorted XML for diffs is unreliable, because converting an XML document to a sorted representation isn't 1:1 mapping.

这篇关于XSLT按名称对节点进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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