【问题标题】:XSLT Add spring bean to beans.xmlXSLT 将 spring bean 添加到 beans.xml
【发布时间】: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


    【解决方案1】:

    您的 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>
    

    【讨论】:

    • “如果”有效。但是这个 xsl 将 bean 添加为:springframework.org/schema/beans" class="com.mysite.MyCustomController"/>。没有xmlns属性可以添加吗?
    • @Alexandr83 添加到 xsl:stylesheet 元素 exclude-result-prefixes="bn"
    【解决方案2】:

    正确代码:

    <?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>
    

    【讨论】:

      猜你喜欢
      • 2015-08-07
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      相关资源
      最近更新 更多