简体   繁体   中英

Generating xml from existing one using xslt

I'm using xslt to alter pom.xml at maven runtime for generating resources as a deliverable. I just need entire node for the new xml. Given the xslt and expected output details.

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

Actually Output:

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

Need the whole properties tag to new xml not just the values. Please help me in getting the right xslt.

You can use xsl:copy-of here, instead of xsl:apply-templates

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

Or, you could change it to this, as then it would copy the parent properties element too (So you could remove the <properties> and </properties> from your XSLT.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM