0

I was wondering if it is possible using XSL to merge elements in a document. Specifically for the below example.

I want to merge elements so that the ones are effectively aggregated including any additional descendents (grandchildren) as well.

Note that the key for the Child implicitly includes the parent so 123/ABC is different to 456/ABC.

A nice to know would also be around how to determine the the nodes that "Wins" for ones where an overwrite occurs (such as parent/name).

From this

<?xml version="1.0" encoding="UTF-8"?>
<listOfParent>
<parent>
    <parentId>123</parentId>
    <name>Jim</name>
    <value>some data</value>
    <listOfChild>
        <child>
            <childReference>ABC</childReference>
            <name>Karen</name>
        </child>
    </listOfChild>
</parent>
<parent>
    <parentId>123</parentId>
    <name>Fred</name>
    <listOfChild>
        <child>
            <childReference>ABC</childReference>
            <name>Gemma</name>
        </child>
        <child>
            <childReference>DEF</childReference>
            <name>Karen</name>
        </child>
    </listOfChild>
</parent>
<parent>
    <parentId>456</parentId>
    <name>Bill</name>
    <listOfChild>
        <child>
            <childReference>ABC</childReference>
            <name>Penny</name>
        </child>
        <child>
            <childReference>DEF</childReference>
            <name>Rose</name>
        </child>
    </listOfChild>
</parent>

to this

<?xml version="1.0" encoding="UTF-8"?>
<listOfParent>
<parent>
    <parentId>123</parentId>
    <name>not bothered</name>
    <value>some data</value>
    <listOfChild>
        <child>
            <childReference>ABC</childReference>
            <name>not bothered</name>
        </child>
        <child>
            <childReference>DEF</childReference>
            <name>Karen</name>
        </child>
    </listOfChild>
</parent>
<parent>
    <parentId>456</parentId>
    <name>Bill</name>
    <listOfChild>
        <child>
            <childReference>ABC</childReference>
            <name>Penny</name>
        </child>
        <child>
            <childReference>DEF</childReference>
            <name>Rose</name>
        </child>
    </listOfChild>
</parent>

Here is the XSL I tried

<?altova_samplexml Untitled1.xml?>
<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:key name="kGDByIdKey" match="parent" use="parentId"/>
<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="parent
[generate-id()
=
 generate-id(key('kGDByIdKey', parentId)[1])
 ]">
    <xsl:copy>
        <xsl:apply-templates select="@*|key('kGDByIdKey', parentId)/node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="parent"/>

  • Yes, it can. (Though I presume your intended question was "how can...") – keshlam Mar 18 '14 at 12:47
  • :-) Yes that would be helpful – user1035795 Mar 18 '14 at 14:12
  • I tried using existing examples found on this site that looked similar, with no success – user1035795 Mar 18 '14 at 14:13
  • Show us what you've tried, and we'll point out what you got wrong. (I presume you *did* rewrite those examples to work with your document structure, right?) – keshlam Mar 18 '14 at 14:18
  • Also, you need to explain what the logic is for "not bothered". – keshlam Mar 18 '14 at 14:19
  • http://stackoverflow.com/questions/11900006/group-merge-childs-of-same-nodes-in-xml-xslt Was my starting point By "not bothered" I mean I'll figure out that bit out myself hopefully. In terms of which source value is used in the result. – user1035795 Mar 18 '14 at 14:50
  • http://stackoverflow.com/questions/11900006/group-merge-childs-of-same-nodes-in-xml-xslt Was my starting point, I simply replaced GroupData with parent as follows (still figuring out how to get code into comment) By "not bothered" I mean I'll figure out that bit out myself hopefully. In terms of which source value is used in the result. – user1035795 Mar 18 '14 at 14:57

0 Answers0