XSLT 添加 spring bean 到 beans.xml
XSLT Add spring bean to beans.xml
我是 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"/>
但是! IF 条件不工作。每次我 运行 代码时它都会添加相同的 bean。
我的xsl错了吗?
谢谢。
您的 XML 包含命名空间 http://www.springframework.org/schema/beans
中的元素。您检查元素并将其添加到默认名称空间 ("")。要使事情正常进行,您需要修改代码
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:bn="http://www.springframework.org/schema/beans"
exclude-result-prefixes="bn">
..........
<xsl:if test="not(bn:bean[@class='com.mysite.MyCustomController'])">
<bean class="com.mysite.MyCustomController"
xmlns="http://www.springframework.org/schema/beans"/>
</xsl:if>
.............
</xsl:stylesheet>
正确代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:bn="http://www.springframework.org/schema/beans">
<xsl:output method="xml" indent="yes" encoding="UTF-8" />
<xsl:attribute-set name="bean-attr-list">
<xsl:attribute name="class">com.mysite.MyCustomController</xsl:attribute>
</xsl:attribute-set>
<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(bn:bean[@class='com.mysite.MyCustomController'])">
<xsl:element name="bean" namespace="http://www.springframework.org/schema/beans" use-attribute-sets="bean-attr-list" />
</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我是 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"/>
但是! IF 条件不工作。每次我 运行 代码时它都会添加相同的 bean。
我的xsl错了吗? 谢谢。
您的 XML 包含命名空间 http://www.springframework.org/schema/beans
中的元素。您检查元素并将其添加到默认名称空间 ("")。要使事情正常进行,您需要修改代码
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:bn="http://www.springframework.org/schema/beans"
exclude-result-prefixes="bn">
..........
<xsl:if test="not(bn:bean[@class='com.mysite.MyCustomController'])">
<bean class="com.mysite.MyCustomController"
xmlns="http://www.springframework.org/schema/beans"/>
</xsl:if>
.............
</xsl:stylesheet>
正确代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:bn="http://www.springframework.org/schema/beans">
<xsl:output method="xml" indent="yes" encoding="UTF-8" />
<xsl:attribute-set name="bean-attr-list">
<xsl:attribute name="class">com.mysite.MyCustomController</xsl:attribute>
</xsl:attribute-set>
<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(bn:bean[@class='com.mysite.MyCustomController'])">
<xsl:element name="bean" namespace="http://www.springframework.org/schema/beans" use-attribute-sets="bean-attr-list" />
</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>