abhi
abhi

Reputation: 53

XSLT transformation to group similar tags

I have this XML with players and citizens sections. Each section has multiple person tags.

<?xml version="1.0" encoding="UTF-8"?>
<test>
   <players>
      <person>
         <name>joe</name>
         <age>20</age>
      </person>
      <person>
         <name>sam</name>
         <age>23</age>
      </person>
   </players>
   <citizens>
      <person>
         <name>joe</name>
         <city>ny</city>
      </person>
      <person>
         <name>sam</name>
         <city>london</city>
      </person>
   </citizens>
</test>

Now I want to transform this so that person tags of, players and citizens sections are merged together based on name tag.

This is the output I need.

<?xml version="1.0" encoding="UTF-8"?>
<test>
   <players>
      <person>
         <name>joe</name>
         <age>20</age>
         <city>ny</city>
      </person>
      <person>
         <name>sam</name>
         <age>23</age>
         <city>london</city>
      </person>
   </players>
</test> 

I want to do an XSLT transformation for this. I tried lots of stuff without luck. Appreciate some help for this.

Update: I tried this.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:variable name="citizens" select="/test/citizens"/>
    <xsl:template match="/test/players">
        <players>
            <xsl:apply-templates select="person"/>
        </players>
    </xsl:template>

    <xsl:template match="person">
        <xsl:variable name="data1" select="."/>
        <xsl:variable name="data2" select="/test/citizens/person[name=current()/name]/."/>
        <person>
            <xsl:copy-of select="$data1/*"/>
            <xsl:for-each select="$data2/*">
                <xsl:variable name="element2" select="name(.)"/>
                <xsl:if test="count($data1/*[name()=$element2])=0">
                    <xsl:copy-of select="."/>
                </xsl:if>
            </xsl:for-each>
        </person>
    </xsl:template>
</xsl:stylesheet>

It's almost correct. I just want to get rid of last 2 person tags. Please guide me.

   <players>
      <person>
         <name>joe</name>
         <age>20</age>
         <city>ny</city>
      </person>
      <person>
         <name>sam</name>
         <age>23</age>
         <city>london</city>
      </person>
   </players>
   <person>
      <name>joe</name>
      <city>ny</city>
   </person>
   <person>
      <name>sam</name>
      <city>london</city>
   </person>

Upvotes: 0

Views: 168

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117175

Here's one way you could look at it:

XSLT 2.0

<xsl:stylesheet version="2.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="*"/>

<xsl:key name="person-by-name" match="person" use="name" />

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

<xsl:template match="/test">
    <xsl:copy>
        <xsl:apply-templates select="players"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="person">
    <xsl:copy>
        <xsl:apply-templates/>
        <xsl:copy-of select="key('person-by-name', name, ../../citizens)/city "/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Here's another:

XSLT 2.0

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

<xsl:template match="/test">
    <xsl:copy>
        <players>
            <xsl:for-each-group select="*/person" group-by="name">
                <person>
                    <xsl:copy-of select="name"/>
                    <xsl:copy-of select="current-group()/*[not(self::name)]"/>
                </person>
            </xsl:for-each-group>
        </players>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions