xsl-list
[Top] [All Lists]

Re: node concatenation

2003-03-29 15:16:51
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="{(_at_)name}">
          <xsl:for-each
           select="//attribute[generate-id()
                              =
                               generate-id(key('kAttr',
                                               concat(current()/@name,
                                                      '|',
                                                       @name
                                                      )
                                               )[1]
                                           )
                               ]">
            <attribute name="{(_at_)name}"/>
          </xsl:for-each>
          <xsl:for-each
           select="//element[generate-id()
                              =
                               generate-id(key('kchEl',
                                               concat(current()/@name,
                                                      '|',
                                                       @name
                                                      )
                                               )[1]
                                           )
                               ]">
            <elementRef  name="{(_at_)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(_at_)hotmail(_dot_)com> wrote in message
news:F140yqZchMExMrLveu700037520(_at_)hotmail(_dot_)com(_dot_)(_dot_)(_dot_)
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



<Prev in Thread] Current Thread [Next in Thread>