【发布时间】:2016-12-17 17:35:18
【问题描述】:
我是 xslt 的新手。 我需要将 spring bean 添加到 xml 以防它尚不存在。 所以我尝试了下一个代码(我使用 ant 来运行这段代码):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="UTF-8" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:if test="not(bean[@class='com.mysite.MyCustomController'])">
<bean class="com.mysite.MyCustomController"/>
</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
它可以工作,但添加了带有 xmlns 属性的元素,所以在最终的 XML 文件中看起来像这样:
<bean xmlns="" class="com.mysite.MyCustomController"/>
我期望没有 xmlns 属性的结果,所以我搜索并且我的 xsl 代码变成:
...
<xsl:if test="not(bean[@class='com.mysite.MyCustomController'])">
<bean class="com.mysite.MyCustomController" xmlns="http://www.springframework.org/schema/beans"/>
</xsl:if>
...
现在结果 XML 看起来很好:
<bean class="com.mysite.MyCustomController"/>
但是! 如果条件不起作用。每次我运行代码时它都会添加相同的 bean。
我的 xsl 错了吗? 谢谢。
【问题讨论】:
标签: xml xslt xml-namespaces