[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: node concatenation

Subject: Re: node concatenation
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Sat, 29 Mar 2003 23:16:51 +0100
xslt concatenation
Hi bix,

Just use the Muenchian method.

This transformation:

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

 <xsl:key name="kEl" match="element" use="@name"/>

 <xsl:key name="kAttr" match="attribute"
          use="concat(../@name, '|', @name)"/>

 <xsl:key name="kchEl" match="element"
          use="concat(../@name, '|', @name)"/>

  <xsl:template match="/">
    <els>
      <xsl:for-each
       select="//element[generate-id()
                         =
                          generate-id(key('kEl', @name)[1])
                          ]">
        <element name="{@name}">
          <xsl:for-each
           select="//attribute[generate-id()
                              =
                               generate-id(key('kAttr',
                                               concat(current()/@name,
                                                      '|',
                                                       @name
                                                      )
                                               )[1]
                                           )
                               ]">
            <attribute name="{@name}"/>
          </xsl:for-each>
          <xsl:for-each
           select="//element[generate-id()
                              =
                               generate-id(key('kchEl',
                                               concat(current()/@name,
                                                      '|',
                                                       @name
                                                      )
                                               )[1]
                                           )
                               ]">
            <elementRef  name="{@name}"/>
          </xsl:for-each>
        </element>
      </xsl:for-each>
    </els>
  </xsl:template>
</xsl:stylesheet>


when applied on your source.xml:

<els>
  <element name="a">
    <attribute name="attA1"/>
    <attribute name="attA2"/>
    <element name="aa">
      <attribute name="attAA1"/>
      <attribute name="attAA2"/>
    </element>
  </element>
  <element name="b" />
  <element name="a">
    <attribute name="attA3"/>
    <attribute name="attA4"/>
    <element name="ab">
      <attribute name="attAB1"/>
      <attribute name="attAB2"/>
      <element name="aba">
        <attribute name="attABA1"/>
        <attribute name="attABA2"/>
        <attribute name="attABA3"/>
        <attribute name="attABA4"/>
      </element>
    </element>
  </element>
  <element name="a"/>
  <element name="b">
    <element name="ba"/></element>
</els>

Produces the wanted result:

<els>
   <element name="a">
      <attribute name="attA1"/>
      <attribute name="attA2"/>
      <attribute name="attA3"/>
      <attribute name="attA4"/>
      <elementRef name="aa"/>
      <elementRef name="ab"/>
   </element>
   <element name="aa">
      <attribute name="attAA1"/>
      <attribute name="attAA2"/>
   </element>
   <element name="b">
      <elementRef name="ba"/>
   </element>
   <element name="ab">
      <attribute name="attAB1"/>
      <attribute name="attAB2"/>
      <elementRef name="aba"/>
   </element>
   <element name="aba">
      <attribute name="attABA1"/>
      <attribute name="attABA2"/>
      <attribute name="attABA3"/>
      <attribute name="attABA4"/>
   </element>
   <element name="ba"/>
</els>


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL


"bix xslt" <bix_xslt@xxxxxxxxxxx> wrote in message
news:F140yqZchMExMrLveu700037520@xxxxxxxxxxxxxx
> All,
>
> I believe this may have been covered before with some of the grouping
q/a's
> but I did a few searches both on the fact and the archive, and I was
unable
> to find what I was looking for.
>
> Essentially, I've got something similar to the following:
> ~~~~xml fragment~~~~
> <element name="a">
>   <attribute name="attA1"/>
>   <attribute name="attA2"/>
>   <element name="aa">
>     <attribute name="attAA1"/>
>     <attribute name="attAA2"/>
>   </element>
> </element>
> <element name="b" />
> <element name="a">
>   <attribute name="attA3"/>
>   <attribute name="attA4"/>
>   <element name="ab">
>     <attribute name="attAB1"/>
>     <attribute name="attAB2"/>
>     <element name="aba">
>       <attribute name="attABA1"/>
>       <attribute name="attABA2"/>
>       <attribute name="attABA3"/>
>       <attribute name="attABA4"/>
>     </element>
>   </element>
> </element>
> <element name="a"/>
> <element name="b">
>   <element name="ba"/>
> </element>
>
> I would like to combine all of the similar elements and reference the
> element children into something like this:
> ~~~ xml fragment output ~~~
> <element name="a">
>   <attribute name="attA1"/>
>   <attribute name="attA2"/>
>   <attribute name="attA3"/>
>   <attribute name="attA4"/>
>   <elementRef name="aa"/>
>   <elementRef name="ab"/>
> </element>
> <element name="b">
>   <elementRef name="ba"/>
> </element>
> <element name="aa">
>   <attribute name="attAA1"/>
>   <attribute name="attAA2"/>
> </element>
> <element name="ab">
>   <attribute name="attAB1"/>
>   <attribute name="attAB2"/>
>   <elementRef name="aba">
> </element>
> ....etc
>
> I am attempting to come up with an automated way of storing off what the
DTD
> is of the xml data produced by my database.  My hope is that I can
transform
> my original xml into this newer format (element/attribute), and then
> transform that into a DTD file.  I have an xsl file that works through all
> of the original xml data and produces what you see above.  However, I'm at
a
> loss how to group/concatenate the multiple data outputs.  If there is a
> better way to do this, I'd sure appreciate the help.
>
> Thanks!
> bix
>
>
>
>
>
> _________________________________________________________________
> Add photos to your messages with MSN 8. Get 2 months FREE*.
> http://join.msn.com/?page=features/featuredemail
>
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>
>




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.