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

RE: grouping sequence question

Subject: RE: grouping sequence question
From: TSchutzerWeissmann@xxxxxxxxxxxxxxxx
Date: Fri, 29 Nov 2002 13:59:10 -0000
RE:  grouping sequence question
Hello Paul

[...]
> what if I can't evaluate by string->
> not(start-with(@ref, 'GRP')), but need to judge from
> whether the element can apply another templates.
> because there are lots different groups, and naming by
> different people, etc.
> such as , I add a GRP xs:element in the schema which
> is tailor for this 'GRP2'
> <xs:element name="GRP2">
> 	<xs:complexType>
> 		<xs:sequence>
> 			<xs:element ref="GRP-for-mail"
> maxOccurs="unbounded"/>
> 		</xs:sequence>
> 	</xs:complexType>
> </xs:element>

Nested groups are a bit more complicated, so I'm going to stick with
defining group elements as having a ref attribute that begins with "GRP". 

Let's say you want to output this:
Group starts
---> apply templates to any child group (ie, recurse)
Group ends
Group members.

Since a "child group" isn't going to be a direct child node, you can either
set up an XPath that can cope with the hierarchy of xs:complexType,
xs:sequence, whatever, or you could use a key -

<xsl:key name="childGRPs" match="xs:element[starts-with(@ref, 'GRP')]"
  use="generate-id(ancestor::xs:element[starts-with(@ref, 'GRP')][1])"/>

The [1] makes sure that you only recurse one level at a time, giving you
child groups but no grandchild groups.

The real complication though, is the first key. This is because you don't
want to associate elements with a preceding group that's nested deeper than
they are, so you need to alter the preceding axis and only consider
preceding-siblings or ancestors of preceding-siblings...

<xsl:key name="byGRP"  match="xs:element[not(starts-with(@ref, 'GRP'))]"
  use="generate-id(ancestor-or-self::*/preceding-sibling::xs:element
       [starts-with(@ref, 'GRP')][1])"/>
 
Then you just need to set the recursive template going by sending the first
level of groups to it:




<xsl:template match="/xs:schema/xs:element">
  <!-- for each group -->
  <xsl:for-each select="//xs:element[starts-with(@ref, 'GRP')]">
    <xsl:variable name="me" select="generate-id()"/>

    <xsl:value-of select="@ref"/> begins:
    <xsl:apply-templates select="key('childGRPs',$me)"/>   <!-- recurse over
any children -->
    <xsl:value-of select="@ref"/> ends.


    <xsl:for-each select="key('byGRP',$me)">
      <xsl:value-of select="@ref"/> Starts
    </xsl:for-each>

    <xsl:for-each select="key('byGRP',$me)">
      <xsl:value-of select="@ref"/> Ends
    </xsl:for-each>

  </xsl:for-each>
</xsl:template>

 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.