向节点添加属性 [英] adding attribute to the node
问题描述
如果子节点值等于某个字符串,我正在尝试向节点添加一个属性.
I am trying to add an attribute to the node if the child node value is equal to some string.
我有一个 main.xml 文件
I have a main.xml file
<Employees>
<Employee>
<countryid>32</countryid>
<id name="id">1</id>
<firstname >ABC</firstname>
<lastname >XYZ</lastname>
</Employee>
<Employee>
<countryid>100</countryid>
<id name="id">2</id>
<firstname >ddd</firstname>
<lastname >ggg</lastname>
</Employee>
</Employees>
因此,假设国家 ID 等于 32,那么它应该将属性 country=32 添加到 Employee 节点.输出应如下所示:
So let's say if the country id is equal to 32 then it should add attribute country=32 to Employee node. The output should be like below :
输出.xml
<Employees>
<Employee countryid="32">
<countryid>32</countryid>
<id name="id">1</id>
<firstname >ABC</firstname>
<lastname >XYZ</lastname>
</Employee>
<Employee>
<countryid>100</countryid>
<id name="id">2</id>
<firstname >ddd</firstname>
<lastname >ggg</lastname>
</Employee>
</Employees>
我正在使用以下脚本,但收到错误,在包含元素的子元素之后无法创建属性节点.
I am using the following script but getting error that An attribute node cannot be create after the children of containing element.:
转换.xsl
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="Employees/Employee/countryid[.=32']">
<xsl:attribute name="countryid">32</xsl:attribute>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
任何帮助将不胜感激.我们也可以将 countryid 作为逗号分隔值传递,以便我可以传递 32,100,然后它应该向所有匹配的节点添加属性.
Any help will be appreciated. Also can we pass countryid as comma seprated values so that i can pass 32,100 and then it should add attribute to all the matching nodes.
谢谢.
推荐答案
除了 Dimitre 的好答案,还有一个 XSLT 2.0 样式表:
In addition to Dimitre's good answer, an XSLT 2.0 stylesheet:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="pCountry" select="'32,100'"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Employee[countryid = tokenize($pCountry,',')]">
<Employee countryid="{countryid}">
<xsl:apply-templates select="@*|node()"/>
</Employee>
</xsl:template>
</xsl:stylesheet>
输出:
<Employees>
<Employee countryid="32">
<countryid>32</countryid>
<id name="id">1</id>
<firstname>ABC</firstname>
<lastname>XYZ</lastname>
</Employee>
<Employee countryid="100">
<countryid>100</countryid>
<id name="id">2</id>
<firstname>ddd</firstname>
<lastname>ggg</lastname>
</Employee>
</Employees>
注意:模式中与序列、参数/变量引用的存在比较.
Note: Existencial comparison with sequence, param/variable reference in patterns.
假设 countryid
总是第一个孩子的其他方法:
Other approach assuming countryid
is always first child:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:param name="pCountry" select="'32,100'"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="countryid[. = tokenize($pCountry,',')]">
<xsl:attribute name="countryid">
<xsl:value-of select="."/>
</xsl:attribute>
<xsl:call-template name="identity"/>
</xsl:template>
</xsl:stylesheet>
注意:现在xsl:strip-space
指令很重要(避免在属性之前输出文本节点)
Note: Now xsl:strip-space
instruction is important (avoids output text node before attribute)
这篇关于向节点添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!