【问题标题】:How do I delete an Element with a Sub-Element having a Specific Text with XSLT如何使用 XSLT 删除具有特定文本的子元素的元素
【发布时间】:2016-05-02 15:59:54
【问题描述】:

我想使用 XSLT 删除具有特定子元素和特定文本的元素。用例:我想从 Mavon pom.xml 文件中删除一个插件。

假设(删节的)Maven pom.xml 的 XML 如下:

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

  <groupId>X</groupId>
  <artifactId>Y</artifactId>
  <version>0.8.5-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>Y</name>
  <description>Y.</description>

  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>

    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.eclipse.m2e</groupId>
          <artifactId>lifecycle-mapping</artifactId>
          <version>1.0.0</version>
          <configuration>
            <lifecycleMappingMetadata>
              <pluginExecutions>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-enforcer-plugin</artifactId>
                    <versionRange>[1.0.0,)</versionRange>
                    <goals>
                      <goal>enforce</goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <ignore />
                  </action>
                </pluginExecution>
              </pluginExecutions>
            </lifecycleMappingMetadata>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <version>2.9.1</version>
        <configuration>
          <show>private</show>
          <detectLinks>true</detectLinks>
          <detectJavaApiLink>true</detectJavaApiLink>
          <quiet>true</quiet>
        </configuration>
        <executions>
          <execution>
            <id>attach-javadoc</id>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.3</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>

              <createSourcesJar>true</createSourcesJar>
              <shadeTestJar>true</shadeTestJar>
              <minimizeJar>false</minimizeJar>
              <shadedArtifactAttached>true</shadedArtifactAttached>
              <createDependencyReducedPom>false</createDependencyReducedPom>

              <shadedClassifierName>full</shadedClassifierName>

              <filters>
                <filter>
                  <artifact>*:*</artifact>
                  <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                  </excludes>
                </filter>
              </filters>


              <transformers>
                <transformer
                  implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer" />
                <transformer
                  implementation="org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer" />
                <transformer
                  implementation="org.apache.maven.plugins.shade.resource.PluginXmlResourceTransformer" />
                <transformer
                  implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>

    </plugins>
  </build>

</project>

如何删除maven-shade-plugin 插件?这意味着我需要删除plugin 节点,该节点有一个名为artifactId 的子节点(可能是第一个、第二个或第n 个子节点),其文本为maven-shade-plugin

这会给我留下以下 XML:

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

  <groupId>X</groupId>
  <artifactId>Y</artifactId>
  <version>0.8.5-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>Y</name>
  <description>Y.</description>

  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>

    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.eclipse.m2e</groupId>
          <artifactId>lifecycle-mapping</artifactId>
          <version>1.0.0</version>
          <configuration>
            <lifecycleMappingMetadata>
              <pluginExecutions>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-enforcer-plugin</artifactId>
                    <versionRange>[1.0.0,)</versionRange>
                    <goals>
                      <goal>enforce</goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <ignore />
                  </action>
                </pluginExecution>
              </pluginExecutions>
            </lifecycleMappingMetadata>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <version>2.9.1</version>
        <configuration>
          <show>private</show>
          <detectLinks>true</detectLinks>
          <detectJavaApiLink>true</detectJavaApiLink>
          <quiet>true</quiet>
        </configuration>
        <executions>
          <execution>
            <id>attach-javadoc</id>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

    </plugins>
  </build>

</project>

我已经阅读了 Removing XML Nodes using XSLTUse XSLT/XPATH to select elements having a child element with a specific value 并认为这样的东西可以解决问题,但还没有设法将它们组合在一起。

非常感谢。 托马斯。

P.S.:这个问题不是关于 Maven 或其最佳实践,而是关于 XML/XSLT/XPATH。

【问题讨论】:

    标签: xml xslt xpath


    【解决方案1】:

    啊……似乎命名空间是问题所在:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:pom="http://maven.apache.org/POM/4.0.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*" />
    
    <xsl:template match="@*|node()">
     <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
    </xsl:template>
    
    <xsl:template match="pom:plugin[pom:artifactId='maven-shade-plugin']" />
    
    </xsl:stylesheet>
    

    似乎可以解决问题... ...抱歉浪费了您的时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-26
      • 2018-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多