使用xslt从现有xml生成xml

时间:2018-03-26 14:08:34

标签: xml maven xslt

我正在使用xslt在maven运行时更改pom.xml以生成资源作为可交付物。我只需要整个节点用于新的xml。给出xslt和预期的输出细节。

输入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>
    <groupId>com.fff.sss.platform</groupId>
    <artifactId>Platform-Parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>Platform Parent Project</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jackson.version>2.9.3</jackson.version>
        <jersey.version>2.26</jersey.version>
        <swagger.version>1.5.16</swagger.version>
        <grizzly.version>2.4.2</grizzly.version>
        <!-- keep these two versions in sync, we reference tinkerpop for groovy support in Common, but sqlg is only in Platform -->
        <sqlg.version>1.5.0</sqlg.version>
        <tinkerpop.version>3.3.1</tinkerpop.version>
        <junit.version>4.12</junit.version>
    </properties>
    <profiles>
        <profile>
            <id>default</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <modules>
                <module>archetype</module>
                <module>Tools</module>
                <module>Common</module>
                <module>ExcelParser</module>
                <module>Platform</module>
                <module>PlatformUI</module>
                <module>WebSocketServer</module>
                <module>RochadeExporter</module>
                <module>RochadeImporter</module>
                <module>SchemaUtil</module>
                <module>WorkflowService</module>
                <module>SocialNotifier</module>
                <module>NotificationsCollector</module>
                <module>IDAPlatform</module>

            </modules>
        </profile>


        <profile>
            <id>docker</id>
            <modules>
                <module>Deploy</module>
            </modules>
        </profile>
                <!-- Starts project to prepare installation package -->
        <profile>
            <id>install_pack</id>
            <modules>
                <module>Install</module>
            </modules>
        </profile>
        <!-- Use '-P sign_artifact' to digitally sign list of files -->
        <profile>
            <id>sign_artifact</id>
            <modules>
                <module>SignArtifacts</module>
            </modules>
        </profile>
    </profiles>

</project>

XSLT:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" />
    <xsl:template match="/">
        <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>com.fff.sss.platform</groupId>
            <artifactId>Platform-Parent</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <packaging>pom</packaging>
            <name>Platform Parent Project</name>
            <profiles>
                <profile>
                    <id>default</id>
                    <activation>
                        <activeByDefault>true</activeByDefault>
                    </activation>
                    <modules>
                        <module>Common</module>
                        <module>IDAPlatform</module>
                    </modules>
                </profile>
            </profiles>
            <properties>
                <xsl:apply-templates select="*/*[starts-with(local-name(),'properties')]" />
            </properties>
        </project>
    </xsl:template>

</xsl:stylesheet>

实际输出:

<?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>
<groupId>com.fff.sss.platform</groupId>
<artifactId>Platform-Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Platform Parent Project</name>
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>Common</module>
<module>IDAPlatform</module>
</modules>
</profile>
</profiles>
<properties>
        UTF-8
        2.9.3
        2.26
        1.5.16
        2.4.2

        1.5.0
        3.3.1
        4.12
    </properties>
</project>

需要将整个属性标记添加到新的xml而不仅仅是值。请帮助我获得正确的xslt。

1 个答案:

答案 0 :(得分:1)

您可以在此处使用xsl:copy-of,而不是xsl:apply-templates

<xsl:copy-of select="*/*[starts-with(local-name(),'properties')]/*" />

或者,您可以将其更改为此,因为它也会复制父properties元素(因此您可以从XSLT中删除<properties></properties>

<xsl:copy-of select="*/*[starts-with(local-name(),'properties')]" />