XML aggregation of nodes with same hierarchy

View: New views
4 Messages — Rating Filter:   Alert me  

XML aggregation of nodes with same hierarchy

by Damien BOUCHET-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

I'm facing the following problem :

I have input xml files containing data as follow :
<?xml version="1.0" encoding="UTF-8"?>
<rowsbyHA>
  <HA id="[A3_T2_S1_L0]">
    <row id="9011"/><row id="9012"/>
  </HA>
  <HA id="[A3_T2_S5_L0]">
    <row id="1053"/><row id="1054"/><row id="7055"/><row id="7056"/><row
id="9051"/><row id="9052"/>
    <row id="12053"/><row id="12054"/>
  </HA>
  <HA id="[A3_T2_S5_L0,A3_T2_S5_L1]">
    <row id="2051"/><row id="2052"/><row id="2053"/><row id="2054"/>
  </HA>
  <HA id="[A3_T2_S5_L1]">
    <row id="1051"/><row id="1052"/><row id="3051"/><row id="4501"/><row
id="4502"/><row id="4503"/>
    <row id="4504"/><row id="4505"/><row id="4506"/><row id="4507"/><row
id="4508"/><row id="4509"/>
    <row id="4510"/><row id="4511"/><row id="4512"/><row id="4513"/><row
id="4514"/><row id="4515"/>
    <row id="4516"/><row id="4517"/><row id="4518"/><row id="4519"/><row
id="4520"/><row id="7051"/>
    <row id="7052"/><row id="7053"/><row id="7054"/><row
id="12051"/><row id="12052"/>
  </HA>
</rowsbyHA>

I would like to transform this input into this expected output :

<?xml version="1.0" encoding="UTF-8"?>
<rowsbyHA>
      <A3>
        <T2>
            <S1>
                <L0>
                    <row id="9011"/>
                    <row id="9012"/>
                </L0>
            </S1>
            <S5>
                <L0>
                    <row id="1053"/>
                    <row id="1054"/>
                    <row id="2051"/>
                    <row id="2052"/>
                    <row id="2053"/>
                    <row id="2054"/>
                    <row id="7055"/>
                    <row id="7056"/>
                    <row id="9051"/>
                    <row id="9052"/>
                    <row id="12053"/>
                    <row id="12054"/>
                </L0>
                <L1>
                    <row id="1051"/>
                    <row id="1052"/>
                    <row id="2051"/>
                    <row id="2052"/>
                    <row id="2053"/>
                    <row id="2054"/>
                    <row id="3051"/>
                    <row id="4501"/>
                    <row id="4502"/>
                    <row id="4503"/>
                    <row id="4504"/>
                    <row id="4505"/>
                    <row id="4506"/>
                    <row id="4507"/>
                    <row id="4508"/>
                    <row id="4509"/>
                    <row id="4510"/>
                    <row id="4511"/>
                    <row id="4512"/>
                    <row id="4513"/>
                    <row id="4514"/>
                    <row id="4515"/>
                    <row id="4516"/>
                    <row id="4517"/>
                    <row id="4518"/>
                    <row id="4519"/>
                    <row id="4520"/>
                    <row id="7051"/>
                    <row id="7052"/>
                    <row id="7053"/>
                    <row id="7054"/>
                    <row id="12051"/>
                    <row id="12052"/>
                </L1>
            </S5>
        </T2>
    </A3>
</rowsbyHA>


I tried the following xsl transformation :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output indent="yes" method="xml" media-type="text/xml;
charset=UTF-8"/>
    <xsl:template match="text()">
    <xsl:value-of select="normalize-space()"/>
    </xsl:template>
    <xsl:template match="//rowsbyHA/HA">
        <xsl:variable name="x"
select="substring-after(substring-before(@id,']'),'[')" />
        <xsl:variable name="y" select="position()"></xsl:variable>
        <xsl:choose>
            <xsl:when test="contains($x,',')">
                <xsl:variable name="t1"
select="tokenize(substring-before($x,',' ),'_')"></xsl:variable>
                <xsl:if test="$t1[1]!=''"><xsl:element name="{$t1[1]}">
                    <xsl:if test="string-length($t1[2])=0"><xsl:copy-of
select="row"></xsl:copy-of></xsl:if>
                    <xsl:if test="$t1[2]!=''"><xsl:element name="{$t1[2]}">
                    <xsl:if test="string-length($t1[3])=0"><xsl:copy-of
select="row"></xsl:copy-of></xsl:if>
                    <xsl:if test="$t1[3]!=''"><xsl:element name="{$t1[3]}">
                    <xsl:if test="string-length($t1[4])=0"><xsl:copy-of
select="row"></xsl:copy-of></xsl:if>
                    <xsl:if test="$t1[4]!=''"><xsl:element name="{$t1[4]}">
                    <xsl:copy-of select="row"></xsl:copy-of>
                    </xsl:element></xsl:if>
                    </xsl:element></xsl:if>
                    </xsl:element></xsl:if>
                    </xsl:element></xsl:if>
                <xsl:variable name="t2"
select="tokenize(substring-after($x,',' ),'_')"></xsl:variable>
                <xsl:if test="$t2[1]!=''"><xsl:element name="{$t2[1]}">
                    <xsl:if test="string-length($t2[2])=0"><xsl:copy-of
select="row"></xsl:copy-of></xsl:if>
                    <xsl:if test="$t2[2]!=''"><xsl:element name="{$t2[2]}">
                    <xsl:if test="string-length($t2[3])=0"><xsl:copy-of
select="row"></xsl:copy-of></xsl:if>
                    <xsl:if test="$t2[3]!=''"><xsl:element name="{$t2[3]}">
                    <xsl:if test="string-length($t2[4])=0"><xsl:copy-of
select="row"></xsl:copy-of></xsl:if>
                    <xsl:if test="$t2[4]!=''"><xsl:element name="{$t2[4]}">
                    <xsl:copy-of select="row"></xsl:copy-of>
                    </xsl:element></xsl:if>
                    </xsl:element></xsl:if>
                    </xsl:element></xsl:if>
                    </xsl:element></xsl:if>
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="t3"
select="tokenize($x,'_')"></xsl:variable>
                <xsl:if test="$t3[1]!=''"><xsl:element name="{$t3[1]}">
                    <xsl:if test="string-length($t3[2])=0"><xsl:copy-of
select="row"></xsl:copy-of></xsl:if>
                    <xsl:if test="$t3[2]!=''"><xsl:element name="{$t3[2]}">
                    <xsl:if test="string-length($t3[3])=0"><xsl:copy-of
select="row"></xsl:copy-of></xsl:if>
                    <xsl:if test="$t3[3]!=''"><xsl:element name="{$t3[3]}">
                    <xsl:if test="string-length($t3[4])=0"><xsl:copy-of
select="row"></xsl:copy-of></xsl:if>
                    <xsl:if test="$t3[4]!=''"><xsl:element name="{$t3[4]}">
                    <xsl:copy-of select="row"></xsl:copy-of>
                    </xsl:element></xsl:if>
                    </xsl:element></xsl:if>
                    </xsl:element></xsl:if>
                    </xsl:element></xsl:if>
            </xsl:otherwise>
        </xsl:choose>

        </xsl:template>      
    <!--<xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="@*"></xsl:copy-of>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>    -->
</xsl:stylesheet>

but I have only been able to get this output :

<?xml version="1.0" encoding="UTF-8"?>
<A3>
   <T2>
      <S1>
         <L0>
            <row id="9011"/>
            <row id="9012"/>
         </L0>
      </S1>
   </T2>
</A3>
<A3>
   <T2>
      <S5>
         <L0>
            <row id="1053"/>
            <row id="1054"/>
            <row id="7055"/>
            <row id="7056"/>
            <row id="9051"/>
            <row id="9052"/>
            <row id="12053"/>
            <row id="12054"/>
         </L0>
      </S5>
   </T2>
</A3>
<A3>
   <T2>
      <S5>
         <L0>
            <row id="2051"/>
            <row id="2052"/>
            <row id="2053"/>
            <row id="2054"/>
         </L0>
      </S5>
   </T2>
</A3>
<A3>
   <T2>
      <S5>
         <L1>
            <row id="2051"/>
            <row id="2052"/>
            <row id="2053"/>
            <row id="2054"/>
         </L1>
      </S5>
   </T2>
</A3>
<A3>
   <T2>
      <S5>
         <L1>
            <row id="1051"/>
            <row id="1052"/>
            <row id="3051"/>
            <row id="4501"/>
            <row id="4502"/>
            <row id="4503"/>
            <row id="4504"/>
            <row id="4505"/>
            <row id="4506"/>
            <row id="4507"/>
            <row id="4508"/>
            <row id="4509"/>
            <row id="4510"/>
            <row id="4511"/>
            <row id="4512"/>
            <row id="4513"/>
            <row id="4514"/>
            <row id="4515"/>
            <row id="4516"/>
            <row id="4517"/>
            <row id="4518"/>
            <row id="4519"/>
            <row id="4520"/>
            <row id="7051"/>
            <row id="7052"/>
            <row id="7053"/>
            <row id="7054"/>
            <row id="12051"/>
            <row id="12052"/>
         </L1>
      </S5>
   </T2>
</A3>

I don't find the way to finish the job and aggregate the nodes with same
hierarchy...
Could someone help me on this question ?

Thanks in advanced for your answers,

Damien,


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
saxon-help@...
https://lists.sourceforge.net/lists/listinfo/saxon-help 

files.zip (2K) Download Attachment

Re: XML aggregation of nodes with same hierarchy

by Martin Honnen-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Damien BOUCHET wrote:

> I'm facing the following problem :
>
> I have input xml files containing data as follow :

Please note that general XSLT questions not specific to Saxon should go
to the XSL mailing list http://www.mulberrytech.com/xsl/xsl-list/ or an
XSLT forum like http://p2p.wrox.com/xslt-86/.

As for your problem, with XSLT 2.0 you can create a temporary result
easily and then apply a second transformation step to that temporary
result in one stylesheet:

<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:mf="http://example.com/2009/mf"
   exclude-result-prefixes="xsd mf"
   version="2.0">

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

   <xsl:function name="mf:nest" as="element()*">
     <xsl:param name="ha" as="element()"/>
     <xsl:param name="names" as="xsd:string*"/>
     <xsl:choose>
       <xsl:when test="not(empty($names))">
         <xsl:element name="{$names[1]}">
           <xsl:sequence select="mf:nest($ha, $names[position() gt 1])"/>
         </xsl:element>
       </xsl:when>
       <xsl:otherwise>
         <xsl:apply-templates select="$ha/node()"/>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:function>

   <xsl:function name="mf:group" as="element()*">
     <xsl:param name="input" as="element()*"/>
     <xsl:for-each-group select="$input" group-by="node-name(.)">
       <xsl:element name="{current-grouping-key()}">
         <xsl:choose>
           <xsl:when test="current-group()/*[self::row]">
             <xsl:copy-of select="current-group()/row"/>
           </xsl:when>
           <xsl:otherwise>
             <xsl:sequence select="mf:group(current-group()/*)"/>
           </xsl:otherwise>
         </xsl:choose>
       </xsl:element>
     </xsl:for-each-group>
   </xsl:function>

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

   <xsl:template match="rowsbyHA">
     <xsl:copy>
       <xsl:variable name="t1">
         <xsl:apply-templates/>
       </xsl:variable>
       <!--
       <debug>
         <xsl:copy-of select="$t1"/>
       </debug>
       -->
       <xsl:sequence select="mf:group($t1/*)"/>
     </xsl:copy>
   </xsl:template>

   <xsl:template match="HA">
     <xsl:variable name="this" select="."/>
     <xsl:for-each select="tokenize(translate(@id, '[]', ''), ',')">
       <xsl:sequence select="mf:nest($this, tokenize(., '_'))"/>
     </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>

--

        Martin Honnen
        http://msmvps.com/blogs/martin_honnen/

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
saxon-help@...
https://lists.sourceforge.net/lists/listinfo/saxon-help 

Re: XML aggregation of nodes with same hierarchy

by Michael Kay :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This list isn't really the right place for getting XSLT coding advice
(unless it's something specific to Saxon). I help if I've got time, but I
won't be able to look at this immediately. A much better place is the
xsl-list at mulberrytech.com

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay 


> -----Original Message-----
> From: Damien BOUCHET [mailto:damien.bouchet@...]
> Sent: 23 October 2009 13:07
> To: Mailing list for the SAXON XSLT and XQuery processor
> Subject: [saxon] XML aggregation of nodes with same hierarchy
>
> Hello,
>
> I'm facing the following problem :
>
> I have input xml files containing data as follow :
> <?xml version="1.0" encoding="UTF-8"?>
> <rowsbyHA>
>   <HA id="[A3_T2_S1_L0]">
>     <row id="9011"/><row id="9012"/>
>   </HA>
>   <HA id="[A3_T2_S5_L0]">
>     <row id="1053"/><row id="1054"/><row id="7055"/><row
> id="7056"/><row id="9051"/><row id="9052"/>
>     <row id="12053"/><row id="12054"/>
>   </HA>
>   <HA id="[A3_T2_S5_L0,A3_T2_S5_L1]">
>     <row id="2051"/><row id="2052"/><row id="2053"/><row id="2054"/>
>   </HA>
>   <HA id="[A3_T2_S5_L1]">
>     <row id="1051"/><row id="1052"/><row id="3051"/><row
> id="4501"/><row id="4502"/><row id="4503"/>
>     <row id="4504"/><row id="4505"/><row id="4506"/><row
> id="4507"/><row id="4508"/><row id="4509"/>
>     <row id="4510"/><row id="4511"/><row id="4512"/><row
> id="4513"/><row id="4514"/><row id="4515"/>
>     <row id="4516"/><row id="4517"/><row id="4518"/><row
> id="4519"/><row id="4520"/><row id="7051"/>
>     <row id="7052"/><row id="7053"/><row id="7054"/><row
> id="12051"/><row id="12052"/>
>   </HA>
> </rowsbyHA>
>
> I would like to transform this input into this expected output :
>
> <?xml version="1.0" encoding="UTF-8"?>
> <rowsbyHA>
>       <A3>
>         <T2>
>             <S1>
>                 <L0>
>                     <row id="9011"/>
>                     <row id="9012"/>
>                 </L0>
>             </S1>
>             <S5>
>                 <L0>
>                     <row id="1053"/>
>                     <row id="1054"/>
>                     <row id="2051"/>
>                     <row id="2052"/>
>                     <row id="2053"/>
>                     <row id="2054"/>
>                     <row id="7055"/>
>                     <row id="7056"/>
>                     <row id="9051"/>
>                     <row id="9052"/>
>                     <row id="12053"/>
>                     <row id="12054"/>
>                 </L0>
>                 <L1>
>                     <row id="1051"/>
>                     <row id="1052"/>
>                     <row id="2051"/>
>                     <row id="2052"/>
>                     <row id="2053"/>
>                     <row id="2054"/>
>                     <row id="3051"/>
>                     <row id="4501"/>
>                     <row id="4502"/>
>                     <row id="4503"/>
>                     <row id="4504"/>
>                     <row id="4505"/>
>                     <row id="4506"/>
>                     <row id="4507"/>
>                     <row id="4508"/>
>                     <row id="4509"/>
>                     <row id="4510"/>
>                     <row id="4511"/>
>                     <row id="4512"/>
>                     <row id="4513"/>
>                     <row id="4514"/>
>                     <row id="4515"/>
>                     <row id="4516"/>
>                     <row id="4517"/>
>                     <row id="4518"/>
>                     <row id="4519"/>
>                     <row id="4520"/>
>                     <row id="7051"/>
>                     <row id="7052"/>
>                     <row id="7053"/>
>                     <row id="7054"/>
>                     <row id="12051"/>
>                     <row id="12052"/>
>                 </L1>
>             </S5>
>         </T2>
>     </A3>
> </rowsbyHA>
>
>
> I tried the following xsl transformation :
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="2.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns:xs="http://www.w3.org/2001/XMLSchema">
>     <xsl:output indent="yes" method="xml"
> media-type="text/xml; charset=UTF-8"/>
>     <xsl:template match="text()">
>     <xsl:value-of select="normalize-space()"/>
>     </xsl:template>
>     <xsl:template match="//rowsbyHA/HA">
>         <xsl:variable name="x"
> select="substring-after(substring-before(@id,']'),'[')" />
>         <xsl:variable name="y" select="position()"></xsl:variable>
>         <xsl:choose>
>             <xsl:when test="contains($x,',')">
>                 <xsl:variable name="t1"
> select="tokenize(substring-before($x,',' ),'_')"></xsl:variable>
>                 <xsl:if test="$t1[1]!=''"><xsl:element
> name="{$t1[1]}">
>                     <xsl:if
> test="string-length($t1[2])=0"><xsl:copy-of
> select="row"></xsl:copy-of></xsl:if>
>                     <xsl:if test="$t1[2]!=''"><xsl:element
> name="{$t1[2]}">
>                     <xsl:if
> test="string-length($t1[3])=0"><xsl:copy-of
> select="row"></xsl:copy-of></xsl:if>
>                     <xsl:if test="$t1[3]!=''"><xsl:element
> name="{$t1[3]}">
>                     <xsl:if
> test="string-length($t1[4])=0"><xsl:copy-of
> select="row"></xsl:copy-of></xsl:if>
>                     <xsl:if test="$t1[4]!=''"><xsl:element
> name="{$t1[4]}">
>                     <xsl:copy-of select="row"></xsl:copy-of>
>                     </xsl:element></xsl:if>
>                     </xsl:element></xsl:if>
>                     </xsl:element></xsl:if>
>                     </xsl:element></xsl:if>
>                 <xsl:variable name="t2"
> select="tokenize(substring-after($x,',' ),'_')"></xsl:variable>
>                 <xsl:if test="$t2[1]!=''"><xsl:element
> name="{$t2[1]}">
>                     <xsl:if
> test="string-length($t2[2])=0"><xsl:copy-of
> select="row"></xsl:copy-of></xsl:if>
>                     <xsl:if test="$t2[2]!=''"><xsl:element
> name="{$t2[2]}">
>                     <xsl:if
> test="string-length($t2[3])=0"><xsl:copy-of
> select="row"></xsl:copy-of></xsl:if>
>                     <xsl:if test="$t2[3]!=''"><xsl:element
> name="{$t2[3]}">
>                     <xsl:if
> test="string-length($t2[4])=0"><xsl:copy-of
> select="row"></xsl:copy-of></xsl:if>
>                     <xsl:if test="$t2[4]!=''"><xsl:element
> name="{$t2[4]}">
>                     <xsl:copy-of select="row"></xsl:copy-of>
>                     </xsl:element></xsl:if>
>                     </xsl:element></xsl:if>
>                     </xsl:element></xsl:if>
>                     </xsl:element></xsl:if>
>             </xsl:when>
>             <xsl:otherwise>
>                 <xsl:variable name="t3"
> select="tokenize($x,'_')"></xsl:variable>
>                 <xsl:if test="$t3[1]!=''"><xsl:element
> name="{$t3[1]}">
>                     <xsl:if
> test="string-length($t3[2])=0"><xsl:copy-of
> select="row"></xsl:copy-of></xsl:if>
>                     <xsl:if test="$t3[2]!=''"><xsl:element
> name="{$t3[2]}">
>                     <xsl:if
> test="string-length($t3[3])=0"><xsl:copy-of
> select="row"></xsl:copy-of></xsl:if>
>                     <xsl:if test="$t3[3]!=''"><xsl:element
> name="{$t3[3]}">
>                     <xsl:if
> test="string-length($t3[4])=0"><xsl:copy-of
> select="row"></xsl:copy-of></xsl:if>
>                     <xsl:if test="$t3[4]!=''"><xsl:element
> name="{$t3[4]}">
>                     <xsl:copy-of select="row"></xsl:copy-of>
>                     </xsl:element></xsl:if>
>                     </xsl:element></xsl:if>
>                     </xsl:element></xsl:if>
>                     </xsl:element></xsl:if>
>             </xsl:otherwise>
>         </xsl:choose>
>
>         </xsl:template>      
>     <!--<xsl:template match="*">
>         <xsl:copy>
>             <xsl:copy-of select="@*"></xsl:copy-of>
>             <xsl:apply-templates/>
>         </xsl:copy>
>     </xsl:template>    -->
> </xsl:stylesheet>
>
> but I have only been able to get this output :
>
> <?xml version="1.0" encoding="UTF-8"?>
> <A3>
>    <T2>
>       <S1>
>          <L0>
>             <row id="9011"/>
>             <row id="9012"/>
>          </L0>
>       </S1>
>    </T2>
> </A3>
> <A3>
>    <T2>
>       <S5>
>          <L0>
>             <row id="1053"/>
>             <row id="1054"/>
>             <row id="7055"/>
>             <row id="7056"/>
>             <row id="9051"/>
>             <row id="9052"/>
>             <row id="12053"/>
>             <row id="12054"/>
>          </L0>
>       </S5>
>    </T2>
> </A3>
> <A3>
>    <T2>
>       <S5>
>          <L0>
>             <row id="2051"/>
>             <row id="2052"/>
>             <row id="2053"/>
>             <row id="2054"/>
>          </L0>
>       </S5>
>    </T2>
> </A3>
> <A3>
>    <T2>
>       <S5>
>          <L1>
>             <row id="2051"/>
>             <row id="2052"/>
>             <row id="2053"/>
>             <row id="2054"/>
>          </L1>
>       </S5>
>    </T2>
> </A3>
> <A3>
>    <T2>
>       <S5>
>          <L1>
>             <row id="1051"/>
>             <row id="1052"/>
>             <row id="3051"/>
>             <row id="4501"/>
>             <row id="4502"/>
>             <row id="4503"/>
>             <row id="4504"/>
>             <row id="4505"/>
>             <row id="4506"/>
>             <row id="4507"/>
>             <row id="4508"/>
>             <row id="4509"/>
>             <row id="4510"/>
>             <row id="4511"/>
>             <row id="4512"/>
>             <row id="4513"/>
>             <row id="4514"/>
>             <row id="4515"/>
>             <row id="4516"/>
>             <row id="4517"/>
>             <row id="4518"/>
>             <row id="4519"/>
>             <row id="4520"/>
>             <row id="7051"/>
>             <row id="7052"/>
>             <row id="7053"/>
>             <row id="7054"/>
>             <row id="12051"/>
>             <row id="12052"/>
>          </L1>
>       </S5>
>    </T2>
> </A3>
>
> I don't find the way to finish the job and aggregate the
> nodes with same hierarchy...
> Could someone help me on this question ?
>
> Thanks in advanced for your answers,
>
> Damien,
>


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
saxon-help@...
https://lists.sourceforge.net/lists/listinfo/saxon-help 

Re: XML aggregation of nodes with same hierarchy

by Damien BOUCHET-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

ok thanks for the link and really sorry for the disturbing,

Best regards,

Damien,

Michael Kay a écrit :

> This list isn't really the right place for getting XSLT coding advice
> (unless it's something specific to Saxon). I help if I've got time, but I
> won't be able to look at this immediately. A much better place is the
> xsl-list at mulberrytech.com
>
> Regards,
>
> Michael Kay
> http://www.saxonica.com/
> http://twitter.com/michaelhkay 
>
>
>  
>> -----Original Message-----
>> From: Damien BOUCHET [mailto:damien.bouchet@...]
>> Sent: 23 October 2009 13:07
>> To: Mailing list for the SAXON XSLT and XQuery processor
>> Subject: [saxon] XML aggregation of nodes with same hierarchy
>>
>> Hello,
>>
>> I'm facing the following problem :
>>
>> I have input xml files containing data as follow :
>> <?xml version="1.0" encoding="UTF-8"?>
>> <rowsbyHA>
>>   <HA id="[A3_T2_S1_L0]">
>>     <row id="9011"/><row id="9012"/>
>>   </HA>
>>   <HA id="[A3_T2_S5_L0]">
>>     <row id="1053"/><row id="1054"/><row id="7055"/><row
>> id="7056"/><row id="9051"/><row id="9052"/>
>>     <row id="12053"/><row id="12054"/>
>>   </HA>
>>   <HA id="[A3_T2_S5_L0,A3_T2_S5_L1]">
>>     <row id="2051"/><row id="2052"/><row id="2053"/><row id="2054"/>
>>   </HA>
>>   <HA id="[A3_T2_S5_L1]">
>>     <row id="1051"/><row id="1052"/><row id="3051"/><row
>> id="4501"/><row id="4502"/><row id="4503"/>
>>     <row id="4504"/><row id="4505"/><row id="4506"/><row
>> id="4507"/><row id="4508"/><row id="4509"/>
>>     <row id="4510"/><row id="4511"/><row id="4512"/><row
>> id="4513"/><row id="4514"/><row id="4515"/>
>>     <row id="4516"/><row id="4517"/><row id="4518"/><row
>> id="4519"/><row id="4520"/><row id="7051"/>
>>     <row id="7052"/><row id="7053"/><row id="7054"/><row
>> id="12051"/><row id="12052"/>
>>   </HA>
>> </rowsbyHA>
>>
>> I would like to transform this input into this expected output :
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <rowsbyHA>
>>       <A3>
>>         <T2>
>>             <S1>
>>                 <L0>
>>                     <row id="9011"/>
>>                     <row id="9012"/>
>>                 </L0>
>>             </S1>
>>             <S5>
>>                 <L0>
>>                     <row id="1053"/>
>>                     <row id="1054"/>
>>                     <row id="2051"/>
>>                     <row id="2052"/>
>>                     <row id="2053"/>
>>                     <row id="2054"/>
>>                     <row id="7055"/>
>>                     <row id="7056"/>
>>                     <row id="9051"/>
>>                     <row id="9052"/>
>>                     <row id="12053"/>
>>                     <row id="12054"/>
>>                 </L0>
>>                 <L1>
>>                     <row id="1051"/>
>>                     <row id="1052"/>
>>                     <row id="2051"/>
>>                     <row id="2052"/>
>>                     <row id="2053"/>
>>                     <row id="2054"/>
>>                     <row id="3051"/>
>>                     <row id="4501"/>
>>                     <row id="4502"/>
>>                     <row id="4503"/>
>>                     <row id="4504"/>
>>                     <row id="4505"/>
>>                     <row id="4506"/>
>>                     <row id="4507"/>
>>                     <row id="4508"/>
>>                     <row id="4509"/>
>>                     <row id="4510"/>
>>                     <row id="4511"/>
>>                     <row id="4512"/>
>>                     <row id="4513"/>
>>                     <row id="4514"/>
>>                     <row id="4515"/>
>>                     <row id="4516"/>
>>                     <row id="4517"/>
>>                     <row id="4518"/>
>>                     <row id="4519"/>
>>                     <row id="4520"/>
>>                     <row id="7051"/>
>>                     <row id="7052"/>
>>                     <row id="7053"/>
>>                     <row id="7054"/>
>>                     <row id="12051"/>
>>                     <row id="12052"/>
>>                 </L1>
>>             </S5>
>>         </T2>
>>     </A3>
>> </rowsbyHA>
>>
>>
>> I tried the following xsl transformation :
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <xsl:stylesheet version="2.0"
>> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>> xmlns:xs="http://www.w3.org/2001/XMLSchema">
>>     <xsl:output indent="yes" method="xml"
>> media-type="text/xml; charset=UTF-8"/>
>>     <xsl:template match="text()">
>>     <xsl:value-of select="normalize-space()"/>
>>     </xsl:template>
>>     <xsl:template match="//rowsbyHA/HA">
>>         <xsl:variable name="x"
>> select="substring-after(substring-before(@id,']'),'[')" />
>>         <xsl:variable name="y" select="position()"></xsl:variable>
>>         <xsl:choose>
>>             <xsl:when test="contains($x,',')">
>>                 <xsl:variable name="t1"
>> select="tokenize(substring-before($x,',' ),'_')"></xsl:variable>
>>                 <xsl:if test="$t1[1]!=''"><xsl:element
>> name="{$t1[1]}">
>>                     <xsl:if
>> test="string-length($t1[2])=0"><xsl:copy-of
>> select="row"></xsl:copy-of></xsl:if>
>>                     <xsl:if test="$t1[2]!=''"><xsl:element
>> name="{$t1[2]}">
>>                     <xsl:if
>> test="string-length($t1[3])=0"><xsl:copy-of
>> select="row"></xsl:copy-of></xsl:if>
>>                     <xsl:if test="$t1[3]!=''"><xsl:element
>> name="{$t1[3]}">
>>                     <xsl:if
>> test="string-length($t1[4])=0"><xsl:copy-of
>> select="row"></xsl:copy-of></xsl:if>
>>                     <xsl:if test="$t1[4]!=''"><xsl:element
>> name="{$t1[4]}">
>>                     <xsl:copy-of select="row"></xsl:copy-of>
>>                     </xsl:element></xsl:if>
>>                     </xsl:element></xsl:if>
>>                     </xsl:element></xsl:if>
>>                     </xsl:element></xsl:if>
>>                 <xsl:variable name="t2"
>> select="tokenize(substring-after($x,',' ),'_')"></xsl:variable>
>>                 <xsl:if test="$t2[1]!=''"><xsl:element
>> name="{$t2[1]}">
>>                     <xsl:if
>> test="string-length($t2[2])=0"><xsl:copy-of
>> select="row"></xsl:copy-of></xsl:if>
>>                     <xsl:if test="$t2[2]!=''"><xsl:element
>> name="{$t2[2]}">
>>                     <xsl:if
>> test="string-length($t2[3])=0"><xsl:copy-of
>> select="row"></xsl:copy-of></xsl:if>
>>                     <xsl:if test="$t2[3]!=''"><xsl:element
>> name="{$t2[3]}">
>>                     <xsl:if
>> test="string-length($t2[4])=0"><xsl:copy-of
>> select="row"></xsl:copy-of></xsl:if>
>>                     <xsl:if test="$t2[4]!=''"><xsl:element
>> name="{$t2[4]}">
>>                     <xsl:copy-of select="row"></xsl:copy-of>
>>                     </xsl:element></xsl:if>
>>                     </xsl:element></xsl:if>
>>                     </xsl:element></xsl:if>
>>                     </xsl:element></xsl:if>
>>             </xsl:when>
>>             <xsl:otherwise>
>>                 <xsl:variable name="t3"
>> select="tokenize($x,'_')"></xsl:variable>
>>                 <xsl:if test="$t3[1]!=''"><xsl:element
>> name="{$t3[1]}">
>>                     <xsl:if
>> test="string-length($t3[2])=0"><xsl:copy-of
>> select="row"></xsl:copy-of></xsl:if>
>>                     <xsl:if test="$t3[2]!=''"><xsl:element
>> name="{$t3[2]}">
>>                     <xsl:if
>> test="string-length($t3[3])=0"><xsl:copy-of
>> select="row"></xsl:copy-of></xsl:if>
>>                     <xsl:if test="$t3[3]!=''"><xsl:element
>> name="{$t3[3]}">
>>                     <xsl:if
>> test="string-length($t3[4])=0"><xsl:copy-of
>> select="row"></xsl:copy-of></xsl:if>
>>                     <xsl:if test="$t3[4]!=''"><xsl:element
>> name="{$t3[4]}">
>>                     <xsl:copy-of select="row"></xsl:copy-of>
>>                     </xsl:element></xsl:if>
>>                     </xsl:element></xsl:if>
>>                     </xsl:element></xsl:if>
>>                     </xsl:element></xsl:if>
>>             </xsl:otherwise>
>>         </xsl:choose>
>>
>>         </xsl:template>      
>>     <!--<xsl:template match="*">
>>         <xsl:copy>
>>             <xsl:copy-of select="@*"></xsl:copy-of>
>>             <xsl:apply-templates/>
>>         </xsl:copy>
>>     </xsl:template>    -->
>> </xsl:stylesheet>
>>
>> but I have only been able to get this output :
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <A3>
>>    <T2>
>>       <S1>
>>          <L0>
>>             <row id="9011"/>
>>             <row id="9012"/>
>>          </L0>
>>       </S1>
>>    </T2>
>> </A3>
>> <A3>
>>    <T2>
>>       <S5>
>>          <L0>
>>             <row id="1053"/>
>>             <row id="1054"/>
>>             <row id="7055"/>
>>             <row id="7056"/>
>>             <row id="9051"/>
>>             <row id="9052"/>
>>             <row id="12053"/>
>>             <row id="12054"/>
>>          </L0>
>>       </S5>
>>    </T2>
>> </A3>
>> <A3>
>>    <T2>
>>       <S5>
>>          <L0>
>>             <row id="2051"/>
>>             <row id="2052"/>
>>             <row id="2053"/>
>>             <row id="2054"/>
>>          </L0>
>>       </S5>
>>    </T2>
>> </A3>
>> <A3>
>>    <T2>
>>       <S5>
>>          <L1>
>>             <row id="2051"/>
>>             <row id="2052"/>
>>             <row id="2053"/>
>>             <row id="2054"/>
>>          </L1>
>>       </S5>
>>    </T2>
>> </A3>
>> <A3>
>>    <T2>
>>       <S5>
>>          <L1>
>>             <row id="1051"/>
>>             <row id="1052"/>
>>             <row id="3051"/>
>>             <row id="4501"/>
>>             <row id="4502"/>
>>             <row id="4503"/>
>>             <row id="4504"/>
>>             <row id="4505"/>
>>             <row id="4506"/>
>>             <row id="4507"/>
>>             <row id="4508"/>
>>             <row id="4509"/>
>>             <row id="4510"/>
>>             <row id="4511"/>
>>             <row id="4512"/>
>>             <row id="4513"/>
>>             <row id="4514"/>
>>             <row id="4515"/>
>>             <row id="4516"/>
>>             <row id="4517"/>
>>             <row id="4518"/>
>>             <row id="4519"/>
>>             <row id="4520"/>
>>             <row id="7051"/>
>>             <row id="7052"/>
>>             <row id="7053"/>
>>             <row id="7054"/>
>>             <row id="12051"/>
>>             <row id="12052"/>
>>          </L1>
>>       </S5>
>>    </T2>
>> </A3>
>>
>> I don't find the way to finish the job and aggregate the
>> nodes with same hierarchy...
>> Could someone help me on this question ?
>>
>> Thanks in advanced for your answers,
>>
>> Damien,
>>
>>    
>
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> saxon-help mailing list archived at http://saxon.markmail.org/
> saxon-help@...
> https://lists.sourceforge.net/lists/listinfo/saxon-help 
>
>  

* <http://www.thalesgroup.com/security-services>*

 


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
saxon-help@...
https://lists.sourceforge.net/lists/listinfo/saxon-help