使用XSL排序属性 [英] Using XSL to sort attributes

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

问题描述

我想通过名称(不是值)排序每个元素的属性规范化一些XML数据的再presentation。这样做是为了保持文本的差异时被添加的属性最小或去除并prevent不同的编辑器从引入等同变异体。这些XML文件是源代码控制之下,开发商都希望在差异比较的变化,而不诉诸专门的XML工具。

I'm trying to canonicalize the representation of some XML data by sorting each element's attributes by name (not value). The idea is to keep textual differences minimal when attributes are added or removed and to prevent different editors from introducing equivalent variants. These XML files are under source control and developers are wanting to diff the changes without resorting to specialized XML tools.

我很惊讶的的找到一个XSL例子,说明了这一点。基本上我只想身份与排序属性变换。我想出了以下似乎与我的所有测试用例的工作:

I was surprised to not find an XSL example of how to this. Basically I want just the identity transform with sorted attributes. I came up with the following with seems to work in all my test cases:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
  <xsl:template match="*|/|text()|comment()|processing-instruction()">
    <xsl:copy>
    <xsl:for-each select="@*">
        <xsl:sort select="name(.)"/>
        <xsl:copy/>
      </xsl:for-each>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

作为整体XSL的n00b我会AP preciate款式或效率的任何意见。我想这可能有助于它张贴在这里,因为它似乎是至少不是一个普通的例子。

As a total XSL n00b I would appreciate any comments on style or efficiency. I thought it might be helpful to post it here since it seems to be at least not a common example.

推荐答案

通过XSLT是一个功能性的语言做的for-each往往可能是我们人类最简单的途径,但不是最有效的XSLT处理器,因为它们不能全面优化呼叫。

With xslt being a functional language doing a for-each might often be the easiest path for us humans but not the most efficient for XSLT processors since they cannot fully optimize the call.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
  <xsl:template match="*">
    <xsl:copy>
      <xsl:apply-templates select="@*">
        <xsl:sort select="name()"/>
      </xsl:apply-templates>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="@*|comment()|processing-instruction()">
    <xsl:copy />     
  </xsl:template>
</xsl:stylesheet>

这完全是微不足道的在这方面,虽然并作为XSL的n00b我想你解决了这个问题确实非常好。

This is totally trivial in this regards though and as a "XSL n00b" i think you solved the problem very well indeed.

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

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