【发布时间】:2011-01-28 04:29:28
【问题描述】:
如果子节点值等于某个字符串,我正在尝试向节点添加属性。
我有一个 main.xml 文件
<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 节点。输出应如下所示:
输出.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>
我正在使用以下脚本,但出现无法在包含元素的子元素之后创建属性节点的错误。:
Transform.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,然后它应该向所有匹配节点添加属性。
谢谢。
【问题讨论】: