Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create parent-child elements based on attribute-values and suppress duplicate elements in output

Tags:

xml

xslt

xslt-1.0

Being a newbie at XSLT, I'm trying to transform - using XSLT 1.0 - the following XML which describes objects:

<Data>
    <Object>
        <Property Name="Id" Value="001"/>
        <Property Name="P.Id" Value="Id P"/>
        <Property Name="P.Description" Value="Descr P"/>
        <Property Name="A.Id" Value="Id A" />
        <Property Name="A.Description" Value="Descr A"/>
        <Property Name="B.Id" Value="Id B"/>
        <Property Name="B.Description" Value="Descr B"/>
        <Property Name="C.Id" Value="" />
        <Property Name="C.Description" Value=""/>
    </Object>
    <Object>
        <Property Name="Id" Value="002"/>
        <Property Name="P.Id" Value="Id P"/>
        <Property Name="P.Description" Value="Descr P"/>
        <Property Name="A.Id" Value="" />
        <Property Name="A.Description" Value=""/>
        <Property Name="B.Id" Value="Id B"/>
        <Property Name="B.Description" Value="Descr B"/>
        <Property Name="C.Id" Value="Id C" />
        <Property Name="C.Description" Value="Descr C"/>
    </Object>
</Data>

The following rules should apply to get the desired output:

  1. For each 'Property'-element that does not contain separator '.' in the 'Name'-attribute, transform the 'Name'-attribute into a child-element and select the value of its 'Value'-attribute.
  2. For each 'Property'-element that does contain separator '.' in the 'Name'-attribute, create:
    • a) a parent element using 'substring-before' the separator in the 'Name'-attribute, and
    • b) a child element using 'substring-after' the separator in the 'Name'-attribute and select the value of its 'Value'-attribute.
  3. Additional rules to (2):
    • a) If 'substring-before' in the 'Name'-attribute to be created, exists in a predefined array and 'Value'-attribute has a value, replace output element-name with a predefined element-name.
    • b) For all elements that (3a) applies, only return the first occurence in the output - i.e. skip following elements that also might occur in the array.

The desired output should therefore look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <ObjectData>
        <Id>001</Id>
        <P>
            <Id>Id P</Id>
            <Description>Descr P</Description>
        </P>
        <Destination>
            <Type>A</Type>
            <Id>Id A</Id>
            <Description>Descr A</Description>
        </Destination>
    </ObjectData>
    <ObjectData>
        <Id>002</Id>
        <P>
            <Id>Id P</Id>
            <Description>Descr P</Description>
        </P>
        <Destination>
            <Type>B</Type>
            <Id>Id B</Id>
            <Description>Descr B</Description>
        </Destination>
    </ObjectData>
</Root>

Currently I have the following code:

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

    <!-- Define keys -->
    <xsl:key name="kPropertyByName" match="Property[contains(@Name, '.')]" use="concat(generate-id(..), '|', substring-before(@Name,'.'))"/>

    <!-- Define variables -->
    <xsl:variable name="vDestinationArray" select="'A,B,C'" />

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

    <!-- Match Data -->
    <xsl:template match="Data" name="Data">
        <xsl:element name="Root">
            <xsl:for-each select="Object">
                <xsl:element name="ObjectData">
                    <xsl:call-template name="Object" />
                </xsl:element>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>

    <!-- Match Object -->
    <xsl:template match="Object" name="Object">
        <!-- For each 'Property'-element that does *not* contain separator '.' in 'Name'-attribute, just select value as-is-->
        <xsl:for-each select="Property[not(contains(@Name, '.'))]">
            <xsl:element name="{@Name}">
                <xsl:value-of select="@Value"/>
            </xsl:element>
        </xsl:for-each>
        <!-- For each 'Property'-element that *does* contain separator '.' in 'Name'-attribute, create a parent element using substring-before separator-->
        <xsl:for-each select="Property[generate-id(.) = generate-id(key('kPropertyByName',concat(generate-id(..), '|', substring-before(@Name,'.')))[1])]">
            <!-- Determine whether parent exists in 'array'-variable -->
            <xsl:choose>
                <!-- Parent *does* exists in 'array'-variable -->
                <xsl:when test="contains(concat(',',$vDestinationArray,','),concat(',',substring-before(@Name,'.'),','))">
                    <xsl:choose>
                        <!-- If value is not empty, create 'Destination'-element -->
                        <xsl:when test="@Value!=''">
                                <xsl:element name="Destination">
                                <xsl:element name="Type">
                                    <xsl:value-of select="substring-before(@Name,'.')" />
                                </xsl:element>
                                <xsl:for-each select="key('kPropertyByName', concat(generate-id(..), '|', substring-before(@Name,'.')))">
                                    <xsl:element name="{substring-after(@Name,'.')}">
                                        <xsl:value-of select="@Value"/>
                                    </xsl:element>
                                </xsl:for-each>                             
                            </xsl:element>
                        </xsl:when>
                    </xsl:choose>
                </xsl:when>
                <!-- Parent does *not* exists in 'array'-variable -->                           
                <xsl:otherwise>
                    <!-- Create child element using substring-after separator -->
                    <xsl:element name="{substring-before(@Name,'.')}">
                        <xsl:for-each select="key('kPropertyByName', concat(generate-id(..), '|', substring-before(@Name,'.')))">
                            <xsl:element name="{substring-after(@Name,'.')}">
                                <xsl:value-of select="@Value"/>
                            </xsl:element>
                        </xsl:for-each>
                    </xsl:element>                          
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Which gives me the following output - having (unwanted) duplicate 'Destination'-elements:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <ObjectData>
        <Id>001</Id>
        <P>
            <Id>Id P</Id>
            <Description>Descr P</Description>
        </P>
        <Destination>
            <Type>A</Type>
            <Id>Id A</Id>
            <Description>Descr A</Description>
        </Destination>
        <Destination>
            <Type>B</Type>
            <Id>Id B</Id>
            <Description>Descr B</Description>
        </Destination>
    </ObjectData>
    <ObjectData>
        <Id>002</Id>
        <P>
            <Id>Id P</Id>
            <Description>Descr P</Description>
        </P>
        <Destination>
            <Type>B</Type>
            <Id>Id B</Id>
            <Description>Descr B</Description>
        </Destination>
        <Destination>
            <Type>C</Type>
            <Id>Id C</Id>
            <Description>Descr C</Description>
        </Destination>
    </ObjectData>
</Root>

Not what I'm looking for... Any help would be much appreciated!

like image 648
user25160809 Avatar asked May 07 '13 18:05

user25160809


2 Answers

Here is a shorter/simpler (no xsl:if, no xsl:key, no generate-id()) solution:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my" extension-element-prefixes="my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>
 <my:names>
   <n>A</n>
   <n>B</n>
   <n>C</n>
 </my:names>

 <xsl:template match="*">
  <Root><xsl:apply-templates/></Root>
 </xsl:template>

 <xsl:template match="/*/*">
  <ObjectData><xsl:apply-templates/></ObjectData>
 </xsl:template>

 <xsl:template match="Property[not(contains(@Name, '.'))]">
  <xsl:element name="{@Name}">
   <xsl:value-of select="@Value"/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="Property">
  <xsl:element name="{substring-before(@Name, '.')}">
    <xsl:element name="{substring-after(@Name, '.')}">
       <xsl:value-of select="@Value"/>
    </xsl:element>
    <xsl:apply-templates mode="descr"  select=
     "../*[@Name = concat(substring-before(current()/@Name, '.'),'.','Description')]"/>
  </xsl:element>
 </xsl:template>

 <xsl:template match=
  "Property[string(@Value) and contains(@Name, '.')
  and substring-before(@Name, '.') = document('')/*/my:names/*]
  [1]
  ">
   <Destination>
       <Type><xsl:value-of select="substring-before(@Name, '.')"/></Type>
       <xsl:element name="{substring-after(@Name, '.')}">
           <xsl:value-of select="@Value"/>
       </xsl:element>
     <xsl:apply-templates mode="descr" select=
     "../*[@Name = concat(substring-before(current()/@Name, '.'),'.','Description')]"/>

   </Destination>
 </xsl:template>

  <xsl:template match=
  "Property[contains(@Name, '.')
          and substring-before(@Name, '.') = document('')/*/my:names/*
          and not(string(@Value))
           ]"/>
  <xsl:template match=
  "Property[contains(@Name, '.')
          and substring-before(@Name, '.') = document('')/*/my:names/*
          and string(@Value)
           ][not(position() = 1)]"/>
 <xsl:template match="*[substring-after(@Name,'.') = 'Description']"/>

 <xsl:template match="*" mode="descr">
  <Description><xsl:apply-templates select="@Value"/></Description>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document:

<Data>
    <Object>
        <Property Name="Id" Value="001"/>
        <Property Name="P.Id" Value="Id P"/>
        <Property Name="P.Description" Value="Descr P"/>
        <Property Name="A.Id" Value="Id A" />
        <Property Name="A.Description" Value="Descr A"/>
        <Property Name="B.Id" Value="Id B"/>
        <Property Name="B.Description" Value="Descr B"/>
        <Property Name="C.Id" Value="" />
        <Property Name="C.Description" Value=""/>
    </Object>
    <Object>
        <Property Name="Id" Value="002"/>
        <Property Name="P.Id" Value="Id P"/>
        <Property Name="P.Description" Value="Descr P"/>
        <Property Name="A.Id" Value="" />
        <Property Name="A.Description" Value=""/>
        <Property Name="B.Id" Value="Id B"/>
        <Property Name="B.Description" Value="Descr B"/>
        <Property Name="C.Id" Value="Id C" />
        <Property Name="C.Description" Value="Descr C"/>
    </Object>
</Data>

the wanted, correct result is produced:

<Root>
   <ObjectData>
      <Id>001</Id>
      <P>
         <Id>Id P</Id>
         <Description>Descr P</Description>
      </P>
      <Destination>
         <Type>A</Type>
         <Id>Id A</Id>
         <Description>Descr A</Description>
      </Destination>
   </ObjectData>
   <ObjectData>
      <Id>002</Id>
      <P>
         <Id>Id P</Id>
         <Description>Descr P</Description>
      </P>
      <Destination>
         <Type>B</Type>
         <Id>Id B</Id>
         <Description>Descr B</Description>
      </Destination>
   </ObjectData>
</Root>
like image 168
Dimitre Novatchev Avatar answered Oct 18 '22 01:10

Dimitre Novatchev


Hopefully something like this is what you are looking for (I reuse parts of your solution):

<xsl:stylesheet  version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" />

    <!-- Define keys -->
    <xsl:key name="kPropertyByName" match="Property[contains(@Name, '.')]" use="concat(generate-id(..), '|', substring-before(@Name,'.'))"/>
    <!-- Define variables -->
    <xsl:variable name="vDestinationArray" select="'A,B,C'" />

    <xsl:template match="Data" >
        <Root>
            <xsl:apply-templates />
        </Root>
    </xsl:template>
    <xsl:template match="Object" >
        <ObjectData>
            <!-- (rule 1.)-->
            <xsl:apply-templates  select="Property[not(contains(@Name, '.'))]"/>

            <!-- For each 'Property'-element that *does* contain separator '.' in 'Name'-attribute,
            and *does* NOT  exists in 'array'-variable 
            (rule 2.)
            -->

            <xsl:for-each
                select="Property[generate-id(.) = 
                        generate-id(key('kPropertyByName',
                    concat(generate-id(..), '|', substring-before(@Name,'.')))[1])
                    and not ( 
                    contains(
                            concat(',',$vDestinationArray,','),concat(',',substring-before(@Name,'.'),','))
                    )
                    ] ">
                    <xsl:apply-templates  select="." mode ="parent" />
            </xsl:for-each>

            <!-- For each 'Property'-element that *does* contain separator '.' in 'Name'-attribute,
            and *does* exists in 'array'-variable
            and Value attribute is not ''
            (rule 3)
            -->
            <xsl:for-each 
                select="Property[generate-id(.) = 
                generate-id(key('kPropertyByName',
                concat(generate-id(..), '|', substring-before(@Name,'.')))[1])
                and
                contains(
                        concat(',',$vDestinationArray,','),concat(',',substring-before(@Name,'.'),','))
                and @Value != ''
                ] ">
                <!-- only for firs one  (rule 3-b.)-->
                <xsl:if test="position() = 1" >
                    <Destination>
                        <xsl:element name="Type">
                            <xsl:value-of select="substring-before(@Name,'.')" />
                        </xsl:element>
                        <xsl:apply-templates
                            mode="replace"
                            select="../Property[
                            substring-before(current()/@Name,'.') = 
                            substring-before(./@Name,'.') 
                            and @Value != '' ]"/>
                    </Destination>
                </xsl:if>
            </xsl:for-each>
        </ObjectData>
    </xsl:template>

    <xsl:template match="Property[not(contains(@Name, '.'))]" >
        <xsl:element name="{@Name}">
            <xsl:value-of select="@Value"/>
        </xsl:element>

    </xsl:template>

    <xsl:template match="Property[@Value != '']" mode ="replace">
            <xsl:element name="{substring-after(@Name,'.')}">
                <xsl:value-of select="@Value"/>
            </xsl:element>
    </xsl:template>

    <xsl:template match="Property[(contains(@Name, '.'))]" mode ="child">
        <xsl:element name="{substring-after(@Name,'.')}">
            <xsl:value-of select="@Value"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="Property[(contains(@Name, '.'))]" mode ="parent">
        <xsl:element name="{substring-before(@Name,'.')}">
            <xsl:apply-templates
                mode="child"
                select="../Property[
                    substring-before(current()/@Name,'.') = 
                    substring-before(./@Name,'.')]"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

This will generate the requested output (as I understood it).

<?xml version="1.0"?>
<Root>
    <ObjectData>
        <Id>001</Id>
        <P>
            <Id>Id P</Id>
            <Description>Descr P</Description>
        </P>
        <Destination>
            <Type>A</Type>
            <Id>Id A</Id>
            <Description>Descr A</Description>
        </Destination>
    </ObjectData>
    <ObjectData>
        <Id>002</Id>
        <P>
            <Id>Id P</Id>
            <Description>Descr P</Description>
        </P>
        <Destination>
            <Type>B</Type>
            <Id>Id B</Id>
            <Description>Descr B</Description>
        </Destination>
    </ObjectData>
</Root>

(This was a little harder than expected. The stylesheet could need a little beautification/ improvement, but it's a little late now.)

like image 31
hr_117 Avatar answered Oct 18 '22 00:10

hr_117