简体   繁体   English

XSLT 1.0排序元素

[英]XSLT 1.0 sort elements

I have the following XML document: 我有以下XML文档:

<?xml version="1.0" encoding="UTF-8"?>
<objects>
    <object>Clutch</object>
    <object>Gearbox</object>
    <object>Cylinder head</object>
    <object>Starter</object>
    <object>Airbox</object>
    <object>Inlet manifold</object>
</objects>

And the following XSLT document: 以下是XSLT文档:

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

    <xsl:template match="objects">
        <parts>
            <xsl:apply-templates>
                <xsl:sort select="object"/>
            </xsl:apply-templates>
        </parts>
    </xsl:template>

    <xsl:template match="object">
        <part>
            <xsl:apply-templates/>
        </part>
    </xsl:template>

</xsl:stylesheet>

When applied I am getting the following output as expected, but it is not being sorted: 应用时,我按预期获得以下输出,但它没有被排序:

<?xml version="1.0" encoding="UTF-8"?>
<parts>
    <part>Clutch</part>
    <part>Gearbox</part>
    <part>Cylinder head</part>
    <part>Starter</part>
    <part>Airbox</part>
    <part>Inlet manifold</part>
</parts>

Why is the <xsl:sort select="object"/> not being applied ? 为什么没有应用<xsl:sort select="object"/>

The reason is here: 原因在于:

  <parts> <xsl:apply-templates> <xsl:sort select="object"/> </xsl:apply-templates> </parts> 

This applies templates to the children ( object ) of the current node ( objects ) and sorts them by the string value of their first object child. 这适用模板的儿童( object当前节点()的objects ),并通过他们的第一个字符串值对其进行排序object的孩子。

However in the provided XML document an object doesn't have any object children -- so they all have the same sort-key -- the empty string -- and their original order isn't changed by the sort operation. 但是,在提供的XML文档中, object没有任何objectobject - 因此它们都具有相同的排序键 - 空字符串 - 并且它们的原始顺序不会被排序操作更改。

Solution : 方案

        <parts>
            <xsl:apply-templates>
                <xsl:sort select="."/>
            </xsl:apply-templates>
        </parts>

The complete transformation becomes : 完整的转变成为

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

 <xsl:template match="objects">
    <parts>
     <xsl:apply-templates>
        <xsl:sort select="."/>
     </xsl:apply-templates>
    </parts>
 </xsl:template>

 <xsl:template match="object">
  <part>
     <xsl:apply-templates/>
  </part>
 </xsl:template>
</xsl:stylesheet>

and when it is applied to the provided XML document: 当它应用于提供的XML文档时:

<objects>
    <object>Clutch</object>
    <object>Gearbox</object>
    <object>Cylinder head</object>
    <object>Starter</object>
    <object>Airbox</object>
    <object>Inlet manifold</object>
</objects>

the wanted, correct result is produced: 产生了想要的正确结果:

<parts>
   <part>Airbox</part>
   <part>Clutch</part>
   <part>Cylinder head</part>
   <part>Gearbox</part>
   <part>Inlet manifold</part>
   <part>Starter</part>
</parts>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM