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 1.0
transformation for this.
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>
Note: I received an answer for XSLT 2.0, But now I'm looking for a solution with XSLT 1.0.