XSLT - 更改属性并同时应用动态ID

时间:2015-07-15 11:32:44

标签: xml xslt xslt-2.0

我有xml文件,如下,

<doc>
  <a ref="Foot"></a>
  <a ref="Foot"></a>
  <a ref="Foot"></a>
  <a ref="End"></a>
  <a ref="End"></a>
  <a ref="End"></a>
  <a ref="Head"></a>
  <a ref="Head"></a>
<doc>

我的要求是将动态增量id属性添加到具有<a>属性的"End"节点。并使用"Foot"更改"End"属性,以便结果文档为

<doc>
  <a ref="End" id="1"></a>
  <a ref="End" id="2"></a>
  <a ref="End" id="3"></a>
  <a ref="End" id="4"></a>
  <a ref="End" id="5"></a>
  <a ref="End" id="6"></a>
  <a ref="Head"></a>
  <a ref="Head"></a>
<doc>

我能够向节点添加动态ID并使用"Foot"更改"End"属性,但ID仅添加到之前具有"End"属性的节点。具有属性"Foot"的节点不添加id。我当前的输出如下,

<doc>
  <a ref="End"></a>
  <a ref="End"></a>
  <a ref="End"></a>
  <a ref="End" id="1"></a>
  <a ref="End" id="2"></a>
  <a ref="End" id="3"></a>
  <a ref="Head"></a>
  <a ref="Head"></a>
<doc>

我的xsl代码如下,

 //change attribute "Foot" to attribute "End" 
 <xsl:template match="a/@ref[. = 'Foot']">
        <xsl:attribute name="id">End</xsl:attribute>
 </xsl:template>


//adds dynamic id's to foot node
<xsl:template match="a/@ref[.='End']">
   <xsl:attribute name="id">
        <xsl:number count="a[@ref='End']" level="any"></xsl:number>
   </xsl:attribute>
</xsl:template>

我的问题是如何将ID添加到先前具有属性"Foot"的前三个节点。 可以将模板应用于<a><xsl:template match="a">)节点并增加id,但这不会起作用,因为如果我执行了<a>具有属性&#34; Head&#34 ;也适用于ID。

我认为我可能会使用xls变量并增加值或者其他东西,但是我在xslt中的经验较少而我想不出一个正确的方法我应该怎么做

任何人都可以建议我一个答案我该怎么做?

提前致谢。

0 个答案:

没有答案