Ravindra Ranwala
Ravindra Ranwala

Reputation: 21124

Removing a parent element in xml while keeping it's children using xslt

I want to transform the following xml,

<pets>
    <Pet>
        <category>
            <id>4</id>
            <name>Lions</name>
        </category>
        <id>9</id>
        <name>Lion 3</name>
        <photoUrls>
            <photoUrl>url1</photoUrl>
            <photoUrl>url2</photoUrl>
        </photoUrls>
        <status>available</status>
        <tags>
            <tag>
                <id>1</id>
                <name>tag3</name>
            </tag>
            <tag>
                <id>2</id>
                <name>tag4</name>
            </tag>
        </tags>
    </Pet>
</pets>

in to this xml format.

<pets>
    <Pet>
        <category>
            <id>4</id>
            <name>Lions</name>
        </category>
        <id>9</id>
        <name>Lion 3</name>
        <photoUrl>url1</photoUrl>
        <photoUrl>url2</photoUrl>
        <status>available</status>
        <tag>
            <id>1</id>
            <name>tag3</name>
        </tag>
        <tag>
            <id>2</id>
            <name>tag4</name>
        </tag>
    </Pet>
</pets>

I tried to write a template as follows, but it removes the parent element with it's children.

<xsl:template match="photoUrls"/>

How can this be done in xslt. Any help is appreciated.

Upvotes: 3

Views: 6722

Answers (3)

Hikmat
Hikmat

Reputation: 460

You can use this code also.

<?xml version="1.0" encoding="UTF-8" ?><xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml"/>

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

Upvotes: 0

har07
har07

Reputation: 89285

I would do it this way :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" method="xml" />

    <xsl:template match="photoUrls|tags">
       <!-- Apply identity transform on child elements of photoUrls/tags-->
        <xsl:apply-templates select="*"/>
    </xsl:template>

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

Upvotes: 5

Ravindra Ranwala
Ravindra Ranwala

Reputation: 21124

The following xslt does the job,

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" method="xml" />

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

    <xsl:template match="photoUrls">
        <xsl:copy-of select="photoUrl" />
    </xsl:template>

    <xsl:template match="tags">
        <xsl:copy-of select="tag" />
    </xsl:template>
</xsl:stylesheet>

But if you have any other way of doing this please don't hesitate to post your answer here.

Upvotes: 3

Related Questions