RE: [xsl] XML Group with XSL

Subject: RE: [xsl] XML Group with XSL
From: aalikaj@xxxxxxxxxxxx
Date: Wed, 21 Apr 2004 13:04:56 -0400
Thank you all for your quick reply,

My Input XML file is little bit more complicated, in my previous example i
wanted to group the chidlren nodes on the same node_1. How can I group the
children nodes of the same "node_3" element??

For example, the input XML is:

<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
      <node_1 attrib1_1="3"
                   attrib1_2="282"
                   attrib1_3="ABC"
                   attrib1_4="BCD"
                   attrib1_5="0">
            <node_21 attrib1_1="3"
                         attrib1_2="282"
                         attrib2_3="1"
                         attrib2_4="1"
                         attrib2_5="BCD">
                  <node_3 attrib1_1="3"
                               attrib1_2="282"
                               attrib2_3="1"
                               attrib3_4="1"
                               attrib3_5="XYZ">
                        <node_41 attrib1_1="3"
                                     attrib1_2="282"
                                     attrib2_3="1"
                                     attrib3_4="1"
                                     node4_5="1"
                                     node4_6="Yes"/>
                        <node_41 attrib1_1="3"
                                     attrib1_2="282"
                                     attrib2_3="1"
                                     attrib3_4="1"
                                     node4_5="2"
                                     node4_6="No"/>
                  </node_3>
                  <node_3 attrib1_1="3"
                               attrib1_2="282"
                               attrib2_3="1"
                               attrib3_4="2"
                               attrib3_5="cvb">
                        <node_41 attrib1_1="3"
                                     attrib1_2="282"
                                     attrib2_3="1"
                                     attrib3_4="2"
                                     node4_5="1"
                                     node4_6="A"/>
                        <node_41 attrib1_1="3"
                                     attrib1_2="282"
                                     attrib2_3="1"
                                     attrib3_4="2"
                                     node4_5="2"
                                     node4_6="B"/>
                  </node_3>
            </node_21>
            <node_21 attrib1_1="3"
                         attrib1_2="282"
                         attrib2_3="2"
                         attrib2_4="1"
                         attrib2_5="bbb">
                  <node_3 attrib1_1="3"
                               attrib1_2="282"
                               attrib2_3="2"
                               attrib3_4="1"
                               attrib3_5="bbb1">
                        <node_41 attrib1_1="3"
                                     attrib1_2="282"
                                     attrib2_3="2"
                                     attrib3_4="1"
                                     node4_5="1"
                                     node4_6="aa"/>
                        <node_41 attrib1_1="3"
                                     attrib1_2="282"
                                     attrib2_3="2"
                                     attrib3_4="1"
                                     node4_5="2"
                                     node4_6="bb"/>
                  </node_3>
                  <node_3 attrib1_1="3"
                               attrib1_2="282"
                               attrib2_3="2"
                               attrib3_4="2"
                               attrib3_5="cvb">
                        <node_41 attrib1_1="3"
                                     attrib1_2="282"
                                     attrib2_3="2"
                                     attrib3_4="2"
                                     node4_5="1"
                                     node4_6="yy"/>
                        <node_41 attrib1_1="3"
                                     attrib1_2="282"
                                     attrib2_3="1"
                                     attrib3_4="2"
                                     node4_5="2"
                                     node4_6="zz"/>
                  </node_3>
            </node_21>

      </node_1>
      <node_1 attrib1_1="3"
                   attrib1_2="282"
                   attrib1_3="ABC"
                   attrib1_4="BCD"
                   attrib1_5="0">
            <node_22 attrib1_1="3"
                         attrib1_2="282"
                         attrib_a1="value_a1"
                         attrib_b1="value_b1"
                         attrib_c1="value_c1"/>
            <node_22 attrib1_1="3"
                         attrib1_2="282"
                         attrib_a2="value_a2"
                         attrib_b2="value_b2"
                         attrib_c2="value_c2"/>
            <node_22 attrib1_1="3"
                         attrib1_2="282"
                         attrib_a3="value_a3"
                         attrib_b3="value_b3"
                         attrib_c3="value_c3"/>
            <node_22 attrib1_1="3"
                         attrib1_2="282"
                         attrib_a4="value_a4"
                         attrib_b4="value_b4"
                         attrib_c4="value_c4"/>

      </node_1>
      <node_1 attrib1_1="3"
                   attrib1_2="282"
                   attrib1_3="ABC"
                   attrib1_4="BCD"
                   attrib1_5="0">

            <node_23 attrib1_1="3"
                         attrib1_2="282"
                         attrib_1="1"/>
            <node_23 attrib1_1="3"
                         attrib1_2="282"
                        attrib_2="2"/>
      </node_1>
      <node_1 attrib1_1="3"
                   attrib1_2="282"
                   attrib1_3="ABC"
                   attrib1_4="BCD"
                   attrib1_5="0">
            <node_21 attrib1_1="3"
                         attrib1_2="282"
                         attrib2_3="2"
                         attrib2_4="1"
                         attrib2_5="bbb">
                  <node_3 attrib1_1="3"
                               attrib1_2="282"
                               attrib2_3="2"
                               attrib3_4="2"
                               attrib3_5="cvb">
                        <node_42 attrib1_1="3"
                                     attrib1_2="282"
                                     attrib2_3="2"
                                     attrib3_4="2"
                                     node_a="1"
                                     node_b="1"
                                     node_c="1"
                                     noded="yy"/>
                        <node_42 attrib1_1="3"
                                     attrib1_2="282"
                                     attrib2_3="1"
                                     attrib3_4="2"
                                     node_a="2"
                                     node_b="2"
                                     node_c="2"
                                     noded="xx"/>
                  </node_3>
            </node_21>

      </node_1>
</ROOT>

and I want it in the following format :

<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
      <node_1 attrib1_1="3"
                   attrib1_2="282"
                   attrib1_3="ABC"
                   attrib1_4="BCD"
                   attrib1_5="0">
            <node_21 attrib1_1="3"
                         attrib1_2="282"
                         attrib2_3="1"
                         attrib2_4="1"
                         attrib2_5="BCD">
                  <node_3 attrib1_1="3"
                               attrib1_2="282"
                               attrib2_3="1"
                               attrib3_4="1"
                               attrib3_5="XYZ">
                        <node_41 attrib1_1="3"
                                     attrib1_2="282"
                                     attrib2_3="1"
                                     attrib3_4="1"
                                     node4_5="1"
                                     node4_6="Yes"/>
                        <node_41 attrib1_1="3"
                                     attrib1_2="282"
                                     attrib2_3="1"
                                     attrib3_4="1"
                                     node4_5="2"
                                     node4_6="No"/>
                  </node_3>
                  <node_3 attrib1_1="3"
                               attrib1_2="282"
                               attrib2_3="1"
                               attrib3_4="2"
                               attrib3_5="cvb">
                        <node_41 attrib1_1="3"
                                     attrib1_2="282"
                                     attrib2_3="1"
                                     attrib3_4="2"
                                     node4_5="1"
                                     node4_6="A"/>
                        <node_41 attrib1_1="3"
                                     attrib1_2="282"
                                     attrib2_3="1"
                                     attrib3_4="2"
                                     node4_5="2"
                                     node4_6="B"/>
                  </node_3>
            </node_21>
            <node_21 attrib1_1="3"
                         attrib1_2="282"
                         attrib2_3="2"
                         attrib2_4="1"
                         attrib2_5="bbb">
                  <node_3 attrib1_1="3"
                               attrib1_2="282"
                               attrib2_3="2"
                               attrib3_4="1"
                               attrib3_5="bbb1">
                        <node_41 attrib1_1="3"
                                     attrib1_2="282"
                                     attrib2_3="2"
                                     attrib3_4="1"
                                     node4_5="1"
                                     node4_6="aa"/>
                        <node_41 attrib1_1="3"
                                     attrib1_2="282"
                                     attrib2_3="2"
                                     attrib3_4="1"
                                     node4_5="2"
                                     node4_6="bb"/>
                  </node_3>
                  <node_3 attrib1_1="3"
                               attrib1_2="282"
                               attrib2_3="2"
                               attrib3_4="2"
                               attrib3_5="cvb">
                        <node_41 attrib1_1="3"
                                     attrib1_2="282"
                                     attrib2_3="2"
                                     attrib3_4="2"
                                     node4_5="1"
                                     node4_6="yy"/>
                        <node_41 attrib1_1="3"
                                     attrib1_2="282"
                                     attrib2_3="1"
                                     attrib3_4="2"
                                     node4_5="2"
                                     node4_6="zz"/>

                        <node_42 attrib1_1="3"
                                     attrib1_2="282"
                                     attrib2_3="2"
                                     attrib3_4="2"
                                     node_a="1"
                                     node_b="1"
                                     node_c="1"
                                     noded="yy"/>
                        <node_42 attrib1_1="3"
                                     attrib1_2="282"
                                     attrib2_3="1"
                                     attrib3_4="2"
                                     node_a="2"
                                     node_b="2"
                                     node_c="2"
                                     noded="xx"/>
                  </node_3>
            </node_21>
            <node_22 attrib1_1="3"
                         attrib1_2="282"
                         attrib_a1="value_a1"
                         attrib_b1="value_b1"
                         attrib_c1="value_c1"/>
            <node_22 attrib1_1="3"
                         attrib1_2="282"
                         attrib_a2="value_a2"
                         attrib_b2="value_b2"
                         attrib_c2="value_c2"/>
            <node_22 attrib1_1="3"
                         attrib1_2="282"
                         attrib_a3="value_a3"
                         attrib_b3="value_b3"
                         attrib_c3="value_c3"/>
            <node_22 attrib1_1="3"
                         attrib1_2="282"
                         attrib_a4="value_a4"
                         attrib_b4="value_b4"
                         attrib_c4="value_c4"/>
            <node_23 attrib1_1="3"
                         attrib1_2="282"
                         attrib_1="1"/>
            <node_23 attrib1_1="3"
                         attrib1_2="282"
                         attrib_2="2"/>

      </node_1>



</ROOT>


As you can notice, for the node_1 and node_3 i want to group all the
children nodes.

Can anybody help me?

Thank you

Ardian Alikaj
Software Developer
Ntuitive Software & Systems.
E: aalikaj@xxxxxxxxxxxx
T: 416.863.9566 x326
F: 416.863.8919


                                                                                                                                      
                      "Andreas L.                                                                                                     
                      Delmelle"                To:       <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>                                            
                      <a_l.delmelle@pan        cc:                                                                                    
                      dora.be>                 Subject:  RE: [xsl] XML Group with XSL                                                 
                                                                                                                                      
                      04/21/2004 12:17                                                                                                
                      PM                                                                                                              
                      Please respond to                                                                                               
                      xsl-list                                                                                                        
                                                                                                                                      
                                                                                                                                      




> -----Original Message-----
> From: Patel, Viral [mailto:viral.patel@xxxxxxxxxxxxxxxxxxxx]
>
> You xsl should look something like:
>

Hi,

The proposed solution won't yield the desired result. The result from your
code would look something like

<ROOT>
  <node_1 ...>
    <node_1 ...>
    <node_1 ...>
  </node_1>
</ROOT>

The intention was good, but it needs to be modified like:

> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> xmlns:fo="http://www.w3.org/1999/XSL/Format";>
>
> <xsl:template match="/">
>            <xsl:text disable-output-escaping="yes">&lt;?xml version="1.0"
> encoding="ISO-8859-1"?&gt;</xsl:text>

??? Why are you explicitly inserting the XML declaration in this way?
If you use <xsl:output method="XML" /> (which is the default BTW), the XML
declaration will be added anyway, and so, if you subsequently use xsl:text
to insert it again, the resulting XML document will be in error.

So, remove the <xsl:text ...> !! If you really need 'ISO-8859-1' encoding
for the result, just add the following as child of xsl:stylesheet :

<xsl:output method="XML" encoding="ISO-8859-1" />

Apply templates only to the first node_1 node:

>   <ROOT>
>     <xsl:apply-templates select="ROOT/node_1[1]" />
>   </ROOT>
>

And in the template matching the node_1 nodes, create a copy, then copy the
attributes, and finally copy all children of the current node as well as
the
children of the other (following-sibling) node_1 nodes :

<xsl:template match="node_1">
  <xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:copy-of select="* | following-sibling::node_1/*" />
  </xsl:copy>
</xsl:template>


Hope this helps!

Cheers,

Andreas

Current Thread