English 中文(简体)
XSL 1. 程序属性
原标题:XSL Process attributes in an order
  • 时间:2012-04-21 03:17:16
  •  标签:
  • xslt

我需要按具体顺序处理属性(涉及任何弊端)。 例如:

<test>
    <event 
        era  ="modern"
        year ="1996"
        quarter = "first"
        day = "13"
        month= "January"
        bcad ="ad"
        hour ="18"
        minute = "23"
        >The big game began.</event>
    <happening 
        era  ="modern"
        day = "18"
        bcad ="ad"
        month= "February"
        hour ="19"
        minute = "24"
        >The big game ended.</happening>
    <other>Before time existed.</other>
</test>

......

<xsl:template match="test//*">
    <div>
        <xsl:apply-templates select="@*" />
        <xsl:apply-templates />
    </div>
</xsl:template>

<xsl:template match="@*">
    <span class="{name()}">
        <xsl:value-of select="."/>
    </span>
</xsl:template>

如果我有此需要,将安排工作。 这就是说,我听了。

<div><span class="era">modern</span>
     <span class="year">1996</span>
     <span class="quarter">first</span>
     <span class="day">13</span>
     <span class="month">January</span>
     <span class="bcad">ad</span>
     <span class="hour">18</span>
     <span class="minute">23</span>The big game began.</div>
<div><span class="era">modern</span>
     <span class="day">18</span>
     <span class="bcad">ad</span>
     <span class="month">February</span>
     <span class="hour">19</span>
     <span class="minute">24</span>The big game ended.</div>
<div>Before time existed.</div>

(尽管没有新路线,我在这里为合法性增添了新内容。)

但是,属性顺序(必然)是正确的。

为确定这一点,我可以修改<代码><xsl:apply-templates selected="@*”/> to <xsl:bet-template name=“atts”/>,并添加一个模板,在所需顺序中采用模板,如:

<xsl:template match="test//*">
    <div>
        <xsl:call-template name="atts" />
        <xsl:apply-templates />
    </div>
</xsl:template>

<xsl:template name="atts">
    <xsl:apply-templates select="@era" />
    <xsl:apply-templates select="@bcad" />
    <xsl:apply-templates select="@year" />
    <xsl:apply-templates select="@quarter" />
    <xsl:apply-templates select="@month" />
    <xsl:apply-templates select="@day" />
    <xsl:apply-templates select="@hour" />
    <xsl:apply-templates select="@minute" />        
</xsl:template>

<xsl:template match="@*">
    <span class="{name()}">
        <xsl:value-of select="."/>
    </span>
</xsl:template>

这是否是按具体顺序处理属性的最佳方法? 我不禁要问,是否有办法使用钥匙或全球变量。

我需要使用XSLT1.0,在实际情况下,有几十个属性,而不仅仅是8个属性。

问题回答

举例而言,属性令在XML中并不重要,即XPath和XSLT可以按任何顺序处理。 因此,强迫某一命令的唯一途径是具体指明它。 一种方式是将它们明确称为你们最后的法典例子。 你们也可以提取所有名词,并将这些名字存放在单独的XML档案中,例如像其他文件一样。

<attributes>
  <attribute>era</attribute>
  <attribute>year</attribute>
  <attribute>month</attribute>
  ...
<attributes>

现在,你可以把这些内容装上文件的功能,并在所有栏目<>中填满:

<xsl:variable name="attributes" select="document( attributes.xml )//attribute"/>
...
<xsl:template match="*">
  <xsl:variable name="self" select="."/>
  <xsl:for-each select="$attributes">
    <xsl:apply-templates select="$self/@*[name()=current()]"/>
  </xsl:for-each>    
</xsl:template>




相关问题
When test hanging in an infinite loop

I m tokenising a string with XSLT 1.0 and trying to prevent empty strings from being recognised as tokens. Here s the entire function, based on XSLT Cookbook: <xsl:template name="tokenize"> ...

quick xslt for-each question

Let s say I have an XML document that has this: <keywords> <keyword>test</keyword> <keyword>test2</keyword> <keyword>test3</keyword> <keyword>test4</...

XSLT Transform XML with Namespaces

I m trying to transform some XML into HTML using XSLT. Problem: I can t get it to work. Can someone tell me what I m doing wrong? XML: <ArrayOfBrokerage xmlns:i="http://www.w3.org/2001/...

XSLT output to HTML

In my XSLT file, I have the following: <input type="button" value= <xsl:value-of select="name">> It s an error as it violates XML rule. What I actually want is having a value from an ...

Mangling IDs and References to IDs in XML

I m trying to compose xml elements into each other, and the problem I am having is when there s the same IDs. Basically what I need to do is mangle all the IDs in an xml file, as well as the ...

Sharepoint 2007 Data view Webpart custom parameters

I m sort of new to the custom parameters that can be setup on a DataView Webpart. There are 6 options: - None - Control - Cookie - Form - QueryString - Server Variable I think that None, Cookie and ...

热门标签