在特定的顺序XSLT显示属性 [英] xslt display attributes in specific order

查看:99
本文介绍了在特定的顺序XSLT显示属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果有一个特定的顺序显示元素的属性,用一用序列或以某种方式建立一种秩序,而不是明确地写它的方式。

I was wondering if there is a way of displaying element's attributes in a specific order, with a use of sequence or somehow establishing an order rather than writing it explicitly.

对不起,解释是有点不清楚。这个例子可以帮助:

Sorry, explanation is a bit unclear. The example might help:

所以我有一个模板:

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

和我想属性不幸出现的顺序1,2,3,4,你不能在garantee XML属性的顺序,也可能是&LT;元素2 =2 4 =43 =31 =1&GT;

And I want attributes to appear in the order 1, 2, 3, 4. Unfortunately, you can't garantee the order of attributes in XML, it could be <Element 2="2" 4="4" 3="3" 1="1">

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

So the template above will produce the following:

<span>2</span>
<span>4</span>
<span>3</span>
<span>1</span>

在理想情况下,我不希望测试的每个属性,如果它得到了一个价值。我在想,如果我能以某种方式把我的显示器的订单?或将我需要明确地这样做,并重复检测,如果在:

Ideally I don't want to test each attribute if it has got a value. I was wondering if I can somehow set an order of my display? Or will I need to do it explicitly and repeating the if test as in:

<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>

很想知道在这种情况下完成的。

Would be interesting to know what can be done in this case.

非常感谢!

推荐答案

在前面一个问题你好像使用XSLT 2.0,所以我希望这一次太的XSLT 2.0解决方案是可能的。该订单是不是在模板的匹配模式决定的,而当你做XSL确定:应用模板。所以(有XSLT 2.0),你可以简单的写的属性的顺序您想例如顺序&LT; XSL:申请模板选择=@ ATT2,@ ATT1,@ ATT3/&GT; 将处理的顺序的属性。随着XSLT 1.0没有序列,但只有节点集所以,如果你想有同样的结果,你需要多个XSL:申请模板在你想例如顺序&LT; XSL:申请模板选择=@ ATT2/&GT;&LT; XSL:申请模板选择=@ ATT1/&GT;&LT; XSL:申请模板选择=@ ATT3 /方式&gt;

In an earlier question you seemed to use XSLT 2.0 so I hope this time too an XSLT 2.0 solution is possible. The order is not determined in the match pattern of a template, rather it is determined when you do xsl:apply-templates. So (with XSLT 2.0) you can simply write a sequence of the attributes in the order you want e.g. <xsl:apply-templates select="@att2, @att1, @att3"/> will process the attributes in that order. With XSLT 1.0 you don't have sequences but only node-sets so there, if you want to have the same result, you need multiple xsl:apply-templates in the order you want e.g. <xsl:apply-templates select="@att2"/><xsl:apply-templates select="@att1"/><xsl:apply-templates select="@att3"/>.

这篇关于在特定的顺序XSLT显示属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆