【问题标题】:xslt more than one insert node if not exists如果不存在,则 xslt 多个插入节点
【发布时间】:2019-01-19 12:25:47
【问题描述】:

我想在一个 Maven 项目中修改我的 pom.xml。我想在它们不存在时插入两个节点(distributionManagement 和配置文件)。当我启动这个 xslt 时,它只插入 distributionManagement 节点,当我再次运行它时,它插入配置文件节点。

这是我的 xslt:

<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:m="http://maven.apache.org/POM/4.0.0" exclude-result-prefixes="m">

<xsl:output method="xml" omit-xml-declaration="no"  indent="yes" />

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
</xsl:template>

<xsl:template match="m:project[not(m:profiles)]">
    <project>
        <xsl:apply-templates select="@*|node()"/>
        <profiles>
            <profile>
                <id>dev</id>
                <properties>
                    <env>devel</env>
                    <snapshot>-SNAPSHOT</snapshot>
                </properties>
            </profile>

            <profile>
                <id>prod</id>
                <properties>
                    <env>prod</env>
                    <snapshot></snapshot>
                </properties>
            </profile>
        </profiles>
    </project>
</xsl:template>

<xsl:template match="m:project[not(m:distributionManagement)]">
    <project>
        <xsl:apply-templates select="@*|node()"/>
        <distributionManagement>
            <snapshotRepository>
                <id>xxxx.repo</id>
                <name>xxxx Nexus snapshot repository</name>
                <url>http://xxx/repository/maven-snapshots/</url>
            </snapshotRepository>
            <repository>
                <id>xxx.repo</id>
                <name>xxxx Nexus repository</name>
                <url>http://xxx/repository/maven-releases/</url>
            </repository>
        </distributionManagement>
    </project>
</xsl:template>

这是一个 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.2.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>TestApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

【问题讨论】:

  • 您是使用像 Saxon 9 这样的 XSLT 2 处理器来运行您的 XSLT 还是使用 XSLT 1 处理器(如 Xalan 或 Oracle JRE 中内置的 Xalan 版本)?一般来说,在一种模式下,您可以只将一个模板应用于一个节点,即使您有两个具有相同优先级的模板都只匹配最后一个使用的模板。
  • 嗨 Martin,是的,我使用 javax.xml.transformer.Transformer 进行转换。是的,我应该将它移动到两个不同的 xsl,但我认为它需要在一个 xsl 中正常工作

标签: xslt


【解决方案1】:

我建议将新值的代码移到参数中,然后,至少在 XSLT 1 中,我认为您可以简单地在模板中检查 project 和两个 xsl:ifs 为您的两个孩子如果它们不存在,则查找并添加它们:

  <xsl:template match="m:project[not(m:profiles) or not(m:distributionManagement)]">
      <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
          <xsl:if test="not(m:profiles)">
              <xsl:copy-of select="$new-profiles"/>
          </xsl:if>
          <xsl:if test="not(m:distributionManagement)">
              <xsl:copy-of select="$new-dist-man"/>
          </xsl:if>
      </xsl:copy>
  </xsl:template>

完整代码

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:m="http://maven.apache.org/POM/4.0.0"
    exclude-result-prefixes="m"
    version="1.0">

  <xsl:output indent="yes"/>

  <xsl:param name="new-dist-man">
        <distributionManagement>
            <snapshotRepository>
                <id>xxxx.repo</id>
                <name>xxxx Nexus snapshot repository</name>
                <url>http://xxx/repository/maven-snapshots/</url>
            </snapshotRepository>
            <repository>
                <id>xxx.repo</id>
                <name>xxxx Nexus repository</name>
                <url>http://xxx/repository/maven-releases/</url>
            </repository>
        </distributionManagement>      
  </xsl:param>

  <xsl:param name="new-profiles">
        <profiles>
            <profile>
                <id>dev</id>
                <properties>
                    <env>devel</env>
                    <snapshot>-SNAPSHOT</snapshot>
                </properties>
            </profile>

            <profile>
                <id>prod</id>
                <properties>
                    <env>prod</env>
                    <snapshot></snapshot>
                </properties>
            </profile>
        </profiles>      
  </xsl:param>

  <xsl:template match="@* | node()">
      <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="m:project[not(m:profiles) or not(m:distributionManagement)]">
      <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
          <xsl:if test="not(m:profiles)">
              <xsl:copy-of select="$new-profiles"/>
          </xsl:if>
          <xsl:if test="not(m:distributionManagement)">
              <xsl:copy-of select="$new-dist-man"/>
          </xsl:if>
      </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

使用 XSLT 3(可能在 Java 中使用来自 Maven 或 Sourceforge 的 Saxon 9.8 或 9.9 HE)它变得更加紧凑,因为参数是正常的序列,我们可以简单地在谓词中测试上下文节点是否没有合适的子节点:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xpath-default-namespace="http://maven.apache.org/POM/4.0.0"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output indent="yes"/>

  <xsl:param name="new-dist-man">
        <distributionManagement>
            <snapshotRepository>
                <id>xxxx.repo</id>
                <name>xxxx Nexus snapshot repository</name>
                <url>http://xxx/repository/maven-snapshots/</url>
            </snapshotRepository>
            <repository>
                <id>xxx.repo</id>
                <name>xxxx Nexus repository</name>
                <url>http://xxx/repository/maven-releases/</url>
            </repository>
        </distributionManagement>      
  </xsl:param>

  <xsl:param name="new-profiles">
        <profiles>
            <profile>
                <id>dev</id>
                <properties>
                    <env>devel</env>
                    <snapshot>-SNAPSHOT</snapshot>
                </properties>
            </profile>

            <profile>
                <id>prod</id>
                <properties>
                    <env>prod</env>
                    <snapshot></snapshot>
                </properties>
            </profile>
        </profiles>      
  </xsl:param>

  <xsl:template match="project[not(profiles) or not(distributionManagement)]">
      <xsl:copy>
          <xsl:apply-templates select="@*, node(), $new-profiles[not(current()/profiles)], $new-dist-man[not(current()/distributionManagement)]"/>
      </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/bFN1y8M/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 2012-12-17
    相关资源
    最近更新 更多