Home > Enterprise >  XSLT 1.0 copy multiple repeating elements to new parents
XSLT 1.0 copy multiple repeating elements to new parents

Time:05-06

I wish to move the some repeating elements to new parent elements. The difficulty I am having is in dealing with elements nested within others I am trying to transform. Example below is a simplified version. Actual XML is much bigger document and there are many more repeated blocks buried inside others. Current XML:

    <root>
    <subject>
        <id>1</id>
        <subjectDetail>
            <address>
                <status>current</status>
                <street>Town Street</street>
                <town>Townsville</town>
            </address>
            <address>
                <status>previous</status>
                <street>Street Lane</street>
                <town>Springtown</town>
            </address>
        </subjectDetail>
    </subject>
    <subject>
        <id>2</id>
        <subjectDetail>
            <address>
                <status>current</status>
                <street>Rose Street</street>
                <town>Gardensville</town>
            </address>
            <address>
                <status>previous</status>
                <street>Violet Lane</street>
                <town>Gardensville</town>
            </address>
        </subjectDetail>
    </subject>
</root>

Desired XML:

<root>    
<subjects>
        <subject>
            <id>1</id>
            <subjectDetail>
                <addresses>
                    <address>
                        <status>current</status>
                        <street>Town Street</street>
                        <town>Townsville</town>
                    </address>
                    <address>
                        <status>previous</status>
                        <street>Street Lane</street>
                        <town>Springtown</town>
                    </address>
                </addresses>
            </subjectDetail>
        </subject>
        <subject>
            <id>2</id>
            <subjectDetail>
                <addresses>
                    <address>
                        <status>current</status>
                        <street>Rose Street</street>
                        <town>Gardensville</town>
                    </address>
                    <address>
                        <status>previous</status>
                        <street>Tulip Street</street>
                        <town>Gardensville</town>
                    </address>
                </addresses>
            </subjectDetail>
        </subject>
    </subjects>
</root>

I'm not sure how to go about this. I've tried nesting the for-each statements but it seems to treat them sequentially and I get duplicated content. Here's what I tried:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="/root/subject">
    <xsl:for-each select="/root/subject">
        <xsl:for-each select="/root/subject/subjectDetail/address">
                <xsl:element name="adresses">
        <xsl:element name="adress">
        <xsl:apply-templates select="@*|node()"/>
                </xsl:element>
        </xsl:element>
            </xsl:for-each>
            <xsl:element name="subjects">
            <xsl:element name="subject">
                <xsl:apply-templates select="@*|node()"/>
            </xsl:element>
            </xsl:element>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

CodePudding user response:

Would something like this work for you:

XSLT 1.0

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

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

<xsl:template match="/root">
    <xsl:copy>
        <subjects>
            <xsl:apply-templates select="subject"/>
        </subjects>
    </xsl:copy>
</xsl:template>

<xsl:template match="subjectDetail">
    <xsl:copy>
        <addresses>
            <xsl:apply-templates select="address"/>
        </addresses>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
  • Related