相关疑难解决方法(0)

XSLT按标记名称和属性值排序

我是XSLT的菜鸟,所以请原谅我的无知...我试图按属性值和标签名称对一个简单的XML文件进行排序,但我很难访问该属性的值.这是一个完整的例子:

<a>
    <b attribute="e"></b>
    <b attribute="b"></b>
    <d attribute="a"></d>
    <c></c>
</a>
Run Code Online (Sandbox Code Playgroud)

预期的结果是:

<a>
    <b attribute="b"></b>
    <b attribute="e"></b>
    <c></c>
    <d attribute="a"></d>
</a>
Run Code Online (Sandbox Code Playgroud)

我试图解决这个问题:

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

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

            <xsl:apply-templates select="node()">
                <xsl:sort select="name()"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

这显然根本不起作用......

在上面的例子中,我想按照它们的属性值对b标签进行排序,但是你可以看到d标签没有按属性值排序,因为它是另一个标签名称......

我想知道使用XSLT是否可行......你有什么想法吗?

提前致谢.

UPDATE ----------------------

我尝试了似乎工作正常并且看起来非常简单的andyb解决方案,但我对此解决方案有另一个问题.

假设我有这个XML:

<a>
    <b attribute="e" optionalAttr="fg"></b>
    <b attribute="b"></b>
    <d attribute="a"></d>
    <c></c>
</a>
Run Code Online (Sandbox Code Playgroud)

我为b标签添加了一个可选参数.应用andyb解决方案将忽略可选参数,因为它在模板中不匹配.结果如下:

<a>
    <b attribute="b"></b>
    <b attribute="e"></b>
    <c></c>
    <d attribute="a"></d>
</a> …
Run Code Online (Sandbox Code Playgroud)

sorting xslt

4
推荐指数
2
解决办法
1万
查看次数

标签 统计

sorting ×1

xslt ×1