如何使用XSLT对XML的子元素进行排序 [英] How to sort a subelement of XML with XSLT

查看:105
本文介绍了如何使用XSLT对XML的子元素进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入XML文件,除了一个子元素包含需要排序的子项之外,我需要将1:1复制到输出中.

I have an input XML file which I need to copy 1:1 to the output, except for one subelement which contains subitems that need to be sorted.

<?xml version="1.0"?>
<top>
  <elementA />
  <elementB />
  <contents>
      <contentitem>
          <id>3</id>
          <moretags1 />
          <moretags2 />
      </contentitem>
      <contentitem>
          <id>2</id>
          <moretags1 />
          <moretags2 />
      </contentitem>
      <contentitem>
          <id>1</id>
          <moretags1 />
          <moretags2 />
      </contentitem>
  </contents>
</top>

我想要一个XSL转换,该转换将"contentitem"元素按其"id"元素排序. 所有其他标签(包括嵌套标签)都必须逐字复制.我已经尝试过使用xsl:copy,但是我得到的是双重内容,或者结果却丢失了.

I'd like an XSL Transformation that puts the "contentitem" elements in order, sorted by their "id" elements. All other tags, including nested ones, must be copied verbatim. I already tried with xsl:copy, but either I get double contents or something turns out missing.

推荐答案

马克·格雷韦尔(Mark Gravell)的解决方案几乎是正确的-出现了一个小问题,即创建了两个嵌套的<contents>元素. 所有提供答案的人的注意事项:请测试您的解决方案

Mark Gravell's solution is almost correct -- with a slight issue that creates two nested <contents> elements. Note to all who provide answers: Do test your solutions!

这是一个正确的解决方案.这种转变:

<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="node()|@*"/>
    </xsl:copy>
  </xsl:template>
<!--                                                --> 
  <xsl:template match="contents">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="contentitem">
        <xsl:sort select="id" data-type="number"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

应用于原始提供的XML文档:

<top>
    <elementA />
    <elementB />
    <contents>
        <contentitem>
            <id>3</id>
            <moretags1 />
            <moretags2 />
        </contentitem>
        <contentitem>
            <id>2</id>
            <moretags1 />
            <moretags2 />
        </contentitem>
        <contentitem>
            <id>1</id>
            <moretags1 />
            <moretags2 />
        </contentitem>
    </contents>
</top>

产生想要的正确结果:

<top>
   <elementA/>
   <elementB/>
   <contents>
      <contentitem>
         <id>1</id>
         <moretags1/>
         <moretags2/>
      </contentitem>
      <contentitem>
         <id>2</id>
         <moretags1/>
         <moretags2/>
      </contentitem>
      <contentitem>
         <id>3</id>
         <moretags1/>
         <moretags2/>
      </contentitem>
   </contents>
</top>

请注意以下事项:

  1. 身份规则的使用复制所有节点而无需更改.

  1. The use of the identity rule to copy all nodes without change.

如何使用与contents元素

<xsl:sort> 指令的使用展示以特定顺序应用模板的结果,该顺序可能与为处理而选择的节点的文档顺序不同.

The use of the <xsl:sort> instruction to present the results of applying a template in a specific order, possibly different from the document order of the nodes, selected for processing.

这篇关于如何使用XSLT对XML的子元素进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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