按输出的特定顺序对属性排序

Das*_*una 4 xslt attributes

如何在不明确写入的情况下以特定顺序编写元素属性?

考虑:

<xsl:template match="Element/@1|@2|@3|@4">
    <xsl:if test="string(.)">
        <span>
            <xsl:value-of select="."/><br/>
        </span>
    </xsl:if>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

属性应显示在订单中1, 2, 3, 4.不幸的是,你不能保证XML中的属性顺序,它可能是<Element 2="2" 4="4" 3="3" 1="1">

所以上面的模板将产生以下内容:

<span>2</span>
<span>4</span>
<span>3</span>
<span>1</span>
Run Code Online (Sandbox Code Playgroud)

理想情况下,如果每个属性都有值,我不想测试它们.我想知道我是否能以某种方式设置显示器的顺序?或者我是否需要明确地执行此操作并重复if测试,如下所示:

<xsl:template match="Element">

    <xsl:if test="string(./@1)>
        <span>
            <xsl:value-of select="./@1"/><br/>
        </span>
    </xsl:if>
    ...
    <xsl:if test="string(./@4)>
        <span>
            <xsl:value-of select="./@4"/><br/>
        </span>
    </xsl:if>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

在这种情况下可以做些什么?

Mar*_*nen 6

在之前的一个问题中,您似乎使用了XSLT 2.0,所以我希望这次也可以使用XSLT 2.0解决方案.

订单不是在模板的匹配模式中确定的,而是在您执行xsl:apply-templates时确定.所以(使用XSLT 2.0)您可以按照您想要的顺序简单地编写一系列属性,例如<xsl:apply-templates select="@att2, @att1, @att3"/>将按该顺序处理属性.

XSLT 1.0没有序列,只有节点集.要产生相同的结果,请xsl:apply-templates按要求的顺序使用,例如:

<xsl:apply-templates select="@att2"/>
<xsl:apply-templates select="@att1"/>
<xsl:apply-templates select="@att3"/>
Run Code Online (Sandbox Code Playgroud)