2

I had to group a xml document in xslt 1.0 using Oracle Service Bus.

This is the sample input file(Simplified):

    <?xml version="1.0" encoding="UTF-8"?>
    <EMailData>
       <property name="A">
          <property name="B">
               <property name="C">
                <row>
                   <property name="C1">
                      <value>ValC1</value>
                   </property>
                   <property name="C2">
                      <value>ValC2</value>
                   </property>
                   <property name="C3">
                      <value>Valc3</value>
                   </property>
                   <property name="C4">
                      <value>Valc4</value>
                   </property>
                 </row>  
             </property> 
             <property name="C">
                <row>
                   <property name="C1">
                      <value>ValC1</value>
                   </property>
                   <property name="C2">
                      <value>ValC2</value>
                   </property>
                   <property name="C3">
                      <value>Valc3</value>
                   </property>
                   <property name="C4">
                      <value>Valc4</value>
                   </property>
                 </row>  
             </property> 
             <property name="D">
                <row>
                   <property name="D1">
                      <value>ValD1</value>
                   </property>
                   <property name="D2">
                      <value>VALd2</value>
                   </property>
                   <property name="D3-InnerElement"> //Need to Group this too
                      <row>
                         <property name="Status">
                            <value>Status122</value>
                         </property>
                      </row>
                   </property>
                   <property name="D3-InnerElement">
                      <row>
                         <property name="Status">
                            <value>Status123</value>
                         </property>
                      </row>
                   </property>
                   <property name="D3-InnerElement">
                      <row>
                         <property name="Status">
                            <value>Status124</value>
                         </property>
                      </row>
                   </property>
                </row>
            </property>
             <property name="D">
                <row>
                   <property name="D1">
                      <value>ValD1</value>
                   </property>
                   <property name="D2">
                      <value>VALd2</value>
                   </property>
                   <property name="D3-InnerElement">
                      <row>
                         <property name="Status">
                            <value>Status122</value>
                         </property>
                      </row>
                   </property>
                   <property name="D3-InnerElement">
                      <row>
                         <property name="Status">
                            <value>Status123</value>
                         </property>
                      </row>
                   </property>
                   <property name="D3-InnerElement">
                      <row>
                         <property name="Status">
                            <value>Status124</value>
                         </property>
                      </row>
                   </property>
                </row>
            </property>             
          </property>
       </property>
    </EMailData>

My XSLT Logic:

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

<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>

<xsl:key name="group" match="/*/*/*/property" use="@name"/>

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

<xsl:template match="/*/*/*[property[@name]]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:for-each select="*[generate-id() = generate-id(key('group', @name)[1])]">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates select="key('group', @name)/*"/>
  </xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>

<!--Change for Inner Hierarchy-->

<xsl:key name="inner-group" match="/*/*/*/*/property" use="@name"/>

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

<xsl:template match="/*/*/*/*[property[@name]]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:for-each select="*[generate-id() = generate-id(key('inner-group', @name)[1])]">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates select="key('inner-group', @name)/*"/>
  </xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Anticipated o/p

<?xml version="1.0" encoding="UTF-8"?>
<EMailData>
   <property name="A">
      <property name="B">
         <property name="C">
            <row>
               <property name="C1">
                  <value>ValC1</value>
               </property>
               <property name="C2">
                  <value>ValC2</value>
               </property>
               <property name="C3">
                  <value>Valc3</value>
               </property>
               <property name="C4">
                  <value>Valc4</value>
               </property>
            </row>
            <row>
               <property name="C1">
                  <value>ValC1</value>
               </property>
               <property name="C2">
                  <value>ValC2</value>
               </property>
               <property name="C3">
                  <value>Valc3</value>
               </property>
               <property name="C4">
                  <value>Valc4</value>
               </property>
            </row>
         </property>
         <property name="D">
            <row>
               <property name="D1">
                  <value>ValD1</value>
               </property>
               <property name="D2">
                  <value>VALd2</value>
               </property>
               <property name="D3-InnerElement"> //Need to Group this too
                      <row>
                     <property name="Status">
                        <value>Status122</value>
                     </property>
                  </row>
               </property>
               <property name="D3-InnerElement">
                  <row>
                     <property name="Status">
                        <value>Status123</value>
                     </property>
                  </row>
                  <row>
                     <property name="Status">
                        <value>Status124</value>
                     </property>
                  </row>
               </property>
            </row>
            <row>
               <property name="D1">
                  <value>ValD1</value>
               </property>
               <property name="D2">
                  <value>VALd2</value>
               </property>
               <property name="D3-InnerElement">
                  <row>
                     <property name="Status">
                        <value>Status122</value>
                     </property>
                  </row>
                  <row>
                     <property name="Status">
                        <value>Status123</value>
                     </property>
                  </row>
                  <row>
                     <property name="Status">
                        <value>Status124</value>
                     </property>
                  </row>
               </property>
            </row>
         </property>
      </property>
   </property>
</EMailData>

But The D3-innerelement is not grouped. Tell me Where I went wrong!!

o/p For my XSLT

    <?xml version="1.0" encoding="UTF-8"?>
<EMailData>
   <property name="A">
      <property name="B">
         <property name="C">
            <row>
               <property name="C1">
                  <value>ValC1</value>
               </property>
               <property name="C2">
                  <value>ValC2</value>
               </property>
               <property name="C3">
                  <value>Valc3</value>
               </property>
               <property name="C4">
                  <value>Valc4</value>
               </property>
            </row>
            <row>
               <property name="C1">
                  <value>ValC1</value>
               </property>
               <property name="C2">
                  <value>ValC2</value>
               </property>
               <property name="C3">
                  <value>Valc3</value>
               </property>
               <property name="C4">
                  <value>Valc4</value>
               </property>
            </row>
         </property>
         <property name="D">
            <row>
               <property name="D1">
                  <value>ValD1</value>
               </property>
               <property name="D2">
                  <value>VALd2</value>
               </property>
               <property name="D3-InnerElement"> //Need to Group this too
                          <row>
                     <property name="Status">
                        <value>Status122</value>
                     </property>
                  </row>
               </property>
               <property name="D3-InnerElement">
                  <row>
                     <property name="Status">
                        <value>Status123</value>
                     </property>
                  </row>
               </property>
               <property name="D3-InnerElement">
                  <row>
                     <property name="Status">
                        <value>Status124</value>
                     </property>
                  </row>
               </property>
            </row>
            <row>
               <property name="D1">
                  <value>ValD1</value>
               </property>
               <property name="D2">
                  <value>VALd2</value>
               </property>
               <property name="D3-InnerElement">
                  <row>
                     <property name="Status">
                        <value>Status122</value>
                     </property>
                  </row>
               </property>
               <property name="D3-InnerElement">
                  <row>
                     <property name="Status">
                        <value>Status123</value>
                     </property>
                  </row>
               </property>
               <property name="D3-InnerElement">
                  <row>
                     <property name="Status">
                        <value>Status124</value>
                     </property>
                  </row>
               </property>
            </row>
         </property>
      </property>
   </property>
</EMailData>

Thanks in Advance!

HeisenBerg
  • 127
  • 10
  • Besides posting your expected output, can you explain in words what you want to be done? It takes a while to understand the stylesheet. – Lingamurthy CS Feb 10 '15 at 09:17
  • I just want to group all individual rows inside a particular element under one element itself. As in, without repeating the element C. Just seperate them by rows. This has to be done further down to an element inside. That's where the problem occurst – HeisenBerg Feb 10 '15 at 09:19
  • Not enough of an explanation. Can you please try to elaborate more. Also, I don't see that your input and expected output are in sync, for example, I can see `Status133` as a text node in your expected output, but not in the input. Could you please fix this too. – Lingamurthy CS Feb 10 '15 at 09:23
  • I have edited the code. Am I clear now? I just need to group elements C and D. With multiple rows under them. I am able to do that with the XSLT logic. But the element D has another element(D3-InnerElement) which has to be grouped in the same way. My code does not tackle that problem. Please correct me. – HeisenBerg Feb 10 '15 at 09:33
  • HeisenBerg, what's the connection to this question: http://stackoverflow.com/questions/28429646/xslt-2-0-to-xslt-1-0-conversion? – Mathias Müller Feb 10 '15 at 11:03
  • Hey Mathis. Its a frienda mine. His question is based on a code which cannot process on OSB.! Any solution to the prob mathis? – HeisenBerg Feb 10 '15 at 11:13

2 Answers2

2

And here goes the solution using Muenchian's grouping:

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

<xsl:key name="group" match="property" use="@name"/>

<xsl:template match="/EMailData/property/property | /EMailData/property/property/property/row">
    <xsl:variable name="id" select="generate-id()"/>
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:for-each select="property[count(. | key('group', @name)[$id = generate-id(parent::*)][1]) = 1]">
            <xsl:copy>
                <xsl:copy-of select="@*"/>
                <xsl:apply-templates select="key('group', @name)[$id = generate-id(parent::*)]/*"/>
            </xsl:copy>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>
<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>
Lingamurthy CS
  • 5,412
  • 2
  • 13
  • 21
1

This solution isn't based on Muenchian's grouping, but thought it would be helpful:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="*[property]">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:for-each select="property[not(@name = preceding-sibling::property/@name)]">
            <xsl:copy>
                <xsl:copy-of select="@*"/>
                <xsl:apply-templates select="../property[@name = current()/@name]/*"/>
            </xsl:copy>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>
<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>

Here, the second template is the identity transform template, used to copy all attributes and nodes.

The first template matches elements with at least one property child, or in simple words, "parents of the property elements to be grouped by @name". You may, as well, change the template match to:

<xsl:template match="/EMailData/property/property | /EMailData/property/property/property/row">

The for-each is on the first property with a specific @name value in the current parent(see the condition using preceding-sibling). And for every iteration, templates are applied for all the child elements of property elements with the current(for-each element's) @name, i.e., grouping property elements of a single parent by their @name's value.

The same template is called for the inner property elements, grouping even those by @name.

Lingamurthy CS
  • 5,412
  • 2
  • 13
  • 21
  • Good work Lingamurthy. Thanks. It works fine. Can u please explain it briefly? – HeisenBerg Feb 10 '15 at 11:30
  • Though I couldn't figure out a way to do it using Muenchian's grouping, I'm glad that it helped you. Let me edit my answer with some explanation. – Lingamurthy CS Feb 10 '15 at 11:31
  • @HeisenBerg please check out my second answer using Muenchian's grouping. – Lingamurthy CS Feb 10 '15 at 12:17
  • Both the solutions work fine. But I am amazed with the ease of the first solution. Kicking myself for not getting it in the first go. I feel the first solution is even more efficient. ? – HeisenBerg Feb 10 '15 at 12:23
  • 2
    No, @HeisenBerg, the first solution is far less efficient than using Muenchian's grouping. Keys give faster access to nodes. You can try using a much bigger data and run some cases to feel the difference. – Lingamurthy CS Feb 10 '15 at 12:25