xsl-list
[Top] [All Lists]

[xsl] Processing Recursive Groups

2007-07-25 12:32:17
Hi,

I'm trying to process groups within groups. Basically, if an XSD element refers to a group with <xsd:group ref="X"/> , I would like to expand this to include the actual group (rather than a reference to it).

Im able to do this without any problem, however, there's a snag (as always). I have some group declarations that contain references to itself. My basic script obviously will give a stack overflow as it keeps substituting the same group over and over again.

<xsd:group name="X">
  ...
  <xsd:group ref="X"/> <!-- KEEPS SUBSTITUTING = NO GOOD! -->
</xsd:group>

So my script starts off like this:

<xsl:key name="allGroups" match="xsd:group[string(@name)]" use="@name"/>

When it comes time to process group references within elements I have something like this to take care of the recursion:

     ...
     <xsl:for-each select="xsd:group">
        <xsl:if test="string(@ref)">
           <xsl:choose>
<xsl:when test="not[ancestor::xsd:group[(_at_)name=current()/@ref]]">
                 <xsl:for-each select="key('allGroups',string(@ref))">
                    <xsl:apply-templates select="*"/>
                 </xsl:for-each>
              </xsl:when>
              <xsl:otherwise>
                 <xsl:element name="group">
                    <xsl:attribute name="ref">
                       <xsl:value-of select="@ref"/>
                    </xsl:attribute>
                 </xsl:element>
              </xsl:otherwise>
           </xsl:choose>
        </xsl:if>
     </xsl:for-each>
     ....

BUT, this doesn't work. I'm pretty sure the "when" statement isn't doing it's job properly. For example, if you come across a group reference in an element:

<xsd:element name="blah">
  <xsd:group ref="X"/>
</xsd:element>

And a group defined with a reference to itself:

<xsd:group name="X">
  <xsd:element name="a">
     <xsd:group ref="X"/>
  </xsd:element>
  <xsd:group ref="Y/>
</xsd:group>

<xsd:group name="Y">
  <xsd:element name="b"/>
</xsd:group>

The end result should look like:

<element name="blah">
  <element name="a">
     <group ref="X"/>
  </element>
  <element name="b"/>
</element>

Shouldn't my script substitute group X in the element, and not substitute itself when it sees that its ancestor is itself, and continue substituting group Y (in group X) ??

I hope this makes sense. If there is a technique out there that can take care of this then I can aslo apply that technique to complexTypes that define elements refering to the same complexType.

Thanks for any help. Greatly appreciate it!

Wasiq Shaikh

_________________________________________________________________
Windows Live Hotmail is the next generation of MSN Hotmail.  It?s fast, simple, and safer than ever and best of all ? it?s still free. Try it today! www.newhotmail.ca?icid=WLHMENCA146


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

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