Xslt 使用XSL样式表按名称属性排序

Xslt 使用XSL样式表按名称属性排序,xslt,transformation,Xslt,Transformation,我有一个XML文件,它有许多相同类型的嵌套元素。我希望能够按名称属性值对它们进行排序。下面是该文件的一个示例 <configurations> <configuration xmlns="http://locomotive/bypass/docx" name="managed"> <configuration name="tracking-component"> <configuration name="

我有一个XML文件,它有许多相同类型的嵌套元素。我希望能够按名称属性值对它们进行排序。下面是该文件的一个示例

 <configurations>
    <configuration xmlns="http://locomotive/bypass/docx" name="managed">
        <configuration name="tracking-component">
            <configuration name="disks"/>
            <configuration name="cycles"/>
        </configuration>
        <configuration name="network-component" class="singular">
            <configuration name="data-component"/>
            <configuration name="node"/>
            <configuration name="modal"/>
        </configuration>
    </configuration>
</configurations>

目标是对上述文件进行如下排序:

<configurations>
    <configuration xmlns="http://locomotive/bypass/docx" name="managed">
        <configuration name="tracking-component" class="singular">
            <configuration name="data-component"/>
            <configuration name="modal"/>
            <configuration name="node"/>
        </configuration>
        <configuration name="network-component">
            <configuration name="disks"/>
            <configuration name="cycles"/>
        </configuration>
    </configuration>
</configurations>

我尝试了这个样式表,但它没有输出任何内容:

<xsl:stylesheet version="1.0">    
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

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

    <xsl:template match="//*[local-name() = 'Configuration']">
        <xsl:copy>
            <xsl:apply-templates select="Configuration/@name">
                <xsl:sort select="."/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

以下是我试图制作的内容:

<configurations>
    <configuration xmlns="http://locomotive/bypass/docx" name="managed">
        <configuration name="network" class="singular">
            <configuration name="data-component"/>      
            <configuration name="modal"/>
            <configuration name="node"/>
        </configuration>
        <configuration name="tracking-component">
            <configuration name="cycles"/>
            <configuration name="disks"/>
        </configuration>
    </configuration>
</configurations>

网络组件
属性现在按正确的字母顺序显示在
跟踪组件
属性之前。
网络组件
跟踪组件
中的配置属性也按字母顺序排序

请帮助。

尝试以下方法:

XSLT1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://locomotive/bypass/docx" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="ns1:configuration">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="ns1:configuration">
            <xsl:sort select="@name"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>


注意

  • XML区分大小写:
    配置
    不等于
    配置

  • 处理名称空间中元素的正确方法是使用如图所示的前缀,而不是仅使用本地名称忽略名称空间

  • 结果与你问题中的结果不同,但我相信这是正确的

  • 试着这样做:

    XSLT1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns1="http://locomotive/bypass/docx" >
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="ns1:configuration">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="ns1:configuration">
                <xsl:sort select="@name"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    
    
    

    注意

  • XML区分大小写:
    配置
    不等于
    配置

  • 处理名称空间中元素的正确方法是使用如图所示的前缀,而不是仅使用本地名称忽略名称空间

  • 结果与你问题中的结果不同,但我相信这是正确的


  • 我改进了XML节的缩进,并通过在处理指令的末尾添加一个
    来清理XSLT节
    我改进了XML节的缩进,并通过在处理指令的末尾添加一个
    来清理XSLT节
    谢谢您的回复。它打印内容,但不按名称排序。问题是,包含父配置元素的外部配置与内部子配置元素一起写入,导致结构扁平化。即使对内部元素进行了排序,排序也会丢失,因为父元素也会被写入。@Breenden不,不是这样的。你可以看到它在这里工作:谢谢你的回复。它打印内容,但不按名称排序。问题是,包含父配置元素的外部配置与内部子配置元素一起写入,导致结构扁平化。即使对内部元素进行了排序,排序也会丢失,因为父元素也会被写入。@Breenden不,不是这样的。您可以看到它在这里工作: