XSLT按属性和属性值排序(带排除)

时间:2015-06-18 16:24:43

标签: sorting xslt attributes xslt-1.0 alphabetical

我有以下XML代码:

<module>
    <component>
        <output>
        <output-test>
        <exclude-output/>
        <orderEntry type="specific"/>
        <orderEntry type="other"/>
        <orderEntry type="module" module-name="module1"/>
        <orderEntry type="module" module-name="module3"/>
        <orderEntry type="module" module-name="module2"/>
        <orderEntry type="library" name="library1"/>
        <orderEntry type="library" name="library3"/>
        <orderEntry type="module" module-name="module4"/>
        <orderEntry type="library" name="library2"/>
    </component>
</module>

我想要输出:

<module>
    <component>
        <output>
        <output-test>
        <exclude-output/>
        <orderEntry type="specific"/>
        <orderEntry type="other"/>
        <orderEntry type="module" module-name="module1"/>
        <orderEntry type="module" module-name="module2"/>
        <orderEntry type="module" module-name="module3"/>
        <orderEntry type="module" module-name="module4"/>
        <orderEntry type="library" name="library1"/>
        <orderEntry type="library" name="library2"/>
        <orderEntry type="library" name="library3"/>
    </component>
</module>

1 个答案:

答案 0 :(得分:1)

我不完全确定这是否符合您的要求,但您可以在单独的xsl:apply-templates中明确选择您想要的那些,然后选择其type属性排序的其他人

<xsl:apply-templates select="orderEntry[@type='specific' or @type='other']" />
<xsl:apply-templates select="orderEntry[@type!='specific' and @type!='other']">
   <xsl:sort select="@type" order="descending" />
   <xsl:sort select="@module-name" order="ascending" />
   <xsl:sort select="@name" order="ascending" />
</xsl:apply-templates>

查看当前的预期输出,似乎所有type属性都按降序排列,因此您可以将其简化为此

<xsl:apply-templates select="orderEntry">
    <xsl:sort select="@type" order="descending" />
    <xsl:sort select="@module-name" order="ascending" />
    <xsl:sort select="@name" order="ascending" />
</xsl:apply-templates>

试试这个XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" indent="yes" />
    <xsl:strip-space elements="*" />
    <xsl:template match="component">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()[not(self::orderEntry)]"/>
            <xsl:apply-templates select="orderEntry">
                <xsl:sort select="@type" order="descending" />
                <xsl:sort select="@module-name" order="ascending" />
                <xsl:sort select="@name" order="ascending" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

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

请注意,如果您确实想要按字母顺序对type属性进行排序,则可以执行以下操作:

<xsl:apply-templates select="orderEntry">
    <xsl:sort select="string-length(substring-before('specific,other,module,library',@type))" order="ascending" />
    <xsl:sort select="@module-name" order="ascending" />
    <xsl:sort select="@name" order="ascending" />
</xsl:apply-templates>