我想测试一个标签的存在并根据该结果创建新的节点..是否可以使用XSLT检测(xml)标签的存在?
这是输入XML:
<root>
<tag1>NS</tag1>
<tag2 id="8">NS</tag2>
<test>
<other_tag>text</other_tag>
<main>Y</main>
</test>
<test>
<other_tag>text</other_tag>
</test>
</root>
而所需的输出XML是:
<root>
<tag1>NS</tag1>
<tag2 id="8">NS</tag2>
<test>
<other_tag>text</other_tag>
<Main_Tag>Present</Main_Tag>
</test>
<test>
<other_tag>text</other_tag>
<Main_Tag>Absent</Main_Tag>
</test>
</root>
我知道测试标签的价值,但这对我来说是新东西。
我试图用这个模板: (不工作按要求)
<xsl:template match="test">
<xsl:element name="test">
<xsl:for-each select="/root/test/*">
<xsl:choose>
<xsl:when test="name()='bbb'">
<xsl:element name="Main_Tag">
<xsl:text>Present</xsl:text>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="Main_Tag">
<xsl:text>Absent</xsl:text>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:element>
</xsl:template>
谢谢先生..它完成.. :-) – 2010-01-22 10:40:31
变体#2为+1。更短,重复性更低。 – Tomalak 2010-01-22 11:07:07
对不起,可能是迂腐,但你的代码测试的主要元素的存在与值“Y”,不是吗?当它具有另一个价值但仍然存在时会发生什么? – 2010-01-22 12:28:15