XSLT - 添加动态 ID 和更改属性
XSLT - Add dynamic id and change attribute
我有如下 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>
<doc>
我的要求是向具有 "End"
属性的 <a>
节点添加动态递增 id 属性。并将 "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>
<doc>
我能够将动态 ID 添加到节点并使用 "End" 更改 "Foot" 属性,但 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>
<doc>
我的xsl代码如下,
//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>
//change attribute "Foot" to attribute "End"
<xsl:template match="a/@ref[. = 'Foot']">
<xsl:attribute name="id">End</xsl:attribute>
</xsl:template>
我的问题是如何将 id 添加到前三个节点。我可能可以使用 xsl 变量,但我是 xslt 的新手,我想不出如何使用变量的方法。另外,如果我们可以先将 "Foot" 属性转换为 "End" 属性对话,然后添加 id,那么代码将 运行 正常。我也不知道 xslt 是否可行。
谁能给我一个答案,我该怎么做?
提前致谢。
如果你想按顺序对 all a
个元素进行编号,无论 @ref
属性包含什么,你为什么不简单地做:
<xsl:template match="a">
<a id="{position()}">
<xsl:apply-templates select="@*|node()"/>
</a>
</xsl:template>
或者,如果您还想将它们全部转换为 ref="End"
(并假设它们没有内容),则更简单:
<xsl:template match="a">
<a ref="End" id="{position()}"/>
</xsl:template>
以下是您的操作方法:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output omit-xml-declaration="yes"/>
<!-- Identity transform -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<!-- Add sequential id attribute; set ref attribute value to "End" -->
<xsl:template match="a">
<a ref="End">
<xsl:attribute name="id"><xsl:number/></xsl:attribute>
</a>
</xsl:template>
</xsl:stylesheet>
来自撒克逊的输出:
<doc>
<a ref="End" id="1"/>
<a ref="End" id="2"/>
<a ref="End" id="3"/>
<a ref="End" id="4"/>
<a ref="End" id="5"/>
<a ref="End" id="6"/>
</doc>
我有如下 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>
<doc>
我的要求是向具有 "End"
属性的 <a>
节点添加动态递增 id 属性。并将 "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>
<doc>
我能够将动态 ID 添加到节点并使用 "End" 更改 "Foot" 属性,但 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>
<doc>
我的xsl代码如下,
//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>
//change attribute "Foot" to attribute "End"
<xsl:template match="a/@ref[. = 'Foot']">
<xsl:attribute name="id">End</xsl:attribute>
</xsl:template>
我的问题是如何将 id 添加到前三个节点。我可能可以使用 xsl 变量,但我是 xslt 的新手,我想不出如何使用变量的方法。另外,如果我们可以先将 "Foot" 属性转换为 "End" 属性对话,然后添加 id,那么代码将 运行 正常。我也不知道 xslt 是否可行。
谁能给我一个答案,我该怎么做?
提前致谢。
如果你想按顺序对 all a
个元素进行编号,无论 @ref
属性包含什么,你为什么不简单地做:
<xsl:template match="a">
<a id="{position()}">
<xsl:apply-templates select="@*|node()"/>
</a>
</xsl:template>
或者,如果您还想将它们全部转换为 ref="End"
(并假设它们没有内容),则更简单:
<xsl:template match="a">
<a ref="End" id="{position()}"/>
</xsl:template>
以下是您的操作方法:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output omit-xml-declaration="yes"/>
<!-- Identity transform -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<!-- Add sequential id attribute; set ref attribute value to "End" -->
<xsl:template match="a">
<a ref="End">
<xsl:attribute name="id"><xsl:number/></xsl:attribute>
</a>
</xsl:template>
</xsl:stylesheet>
来自撒克逊的输出:
<doc>
<a ref="End" id="1"/>
<a ref="End" id="2"/>
<a ref="End" id="3"/>
<a ref="End" id="4"/>
<a ref="End" id="5"/>
<a ref="End" id="6"/>
</doc>