如何使用 maven 将新节点附加到现有 XML 文档



我想使用 Maven 自动添加 Spring 声明,即将 bean 节点附加到现有的 XML 文档中。

我尝试将 AntRun 插件与 xmltask 一起使用,但没有成功:bean.xml 被复制,没有插入 bean 节点元素。

我现在回退使用带有 XSL 转换的 maven XML 插件,但仍然无法通过此解决方案实现我的目标:bean.xml 被复制而不插入 bean 节点元素。

打赌我对XML操作缺乏了解,我一直在这里和那里阅读,但我无法弄清楚我错过了什么。

我正在使用以下POM声明来配置Maven XML插件:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>xml-maven-plugin</artifactId>
    <executions>
      <execution>
        <phase>generate-sources</phase>
        <goals>
          <goal>transform</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <transformationSets>
        <transformationSet>
          <dir>${basedir}/src/main/resources/spring</dir>
          <excludes>
            <exclude>spring-context.xml</exclude>
          </excludes>
          <stylesheet>${basedir}/templates/xslt/spring-stylesheet.xslt</stylesheet>
        </transformationSet>
      </transformationSets>
    </configuration>
    <dependencies>
      <dependency>
        <groupId>net.sf.saxon</groupId>
        <artifactId>saxon</artifactId>
        <version>8.7</version>
      </dependency>
    </dependencies>
  </plugin>

以及以下样式表来配置 XML 转换

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.springframework.org/schema/beans"
version="2.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="beans">
    <xsl:copy>
      <xsl:apply-templates select="@* | *"/>
      <xsl:element name="bean"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

我正在获得以下文件(这是我源文档的精确副本)

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="    http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    http://www.springframework.org/schema/aop    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  <bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
      <property name="location" value="classpath:properties/db.properties"/>
  </bean>
  <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
         id="dataSource">
      <property name="url" value="${jdbc.url}"/>
      <property name="driverClassName" value="${jdbc.driverClassName}"/>
      <property name="username" value="${jdbc.username}"/>
      <property name="password" value="${jdbc.password}"/>
  </bean>
  <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
         id="sessionFactory">
      <property name="dataSource">
         <ref bean="dataSource"/>
      </property>
    <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
         </props>
      </property>
      <property name="mappingLocations">
         <value>classpath:hibernate/queries.hbm.xml</value>
      </property>
  </bean>
  <bean class="org.springframework.orm.hibernate4.HibernateTransactionManager"
         id="txManager">
      <property name="sessionFactory" ref="sessionFactory"/>
  </bean>
  <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"
         id="persistenceExceptionTranslationPostProcessor"/>
</beans>

有人可以指出这里有什么问题,或者建议另一种使用 Maven 附加到 XML 资源的方法吗?

使用

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.springframework.org/schema/beans"
xpath-default-namespace="http://www.springframework.org/schema/beans"
version="2.0">

假设您有一个像 Saxon 9 这样的 XSLT 2.0 处理器。使用像 Xalan 这样的 XSLT 1.0 处理器,您需要

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:sb="http://www.springframework.org/schema/beans"
    version="1.0">
  <xsl:template match="sb:beans">
    <xsl:copy>
      <xsl:apply-templates select="@* | *"/>
      <bean/>
    </xsl:copy>
  </xsl:template>

对于当前代码,match="beans"仅匹配具有本地名称的元素,beans命名空间中。

最新更新