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

RE: grouping with xsl:for-each-group

Subject: RE: grouping with xsl:for-each-group
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 21 Apr 2009 09:56:58 +0100
RE:  grouping with xsl:for-each-group
Try:

<xsl:for-each-group select="*" group-adjacent="local-name()">
  <xsl:choose>
    <xsl:when test="starts-with(current-grouping-key(), 'einschub')">
      <einschub typ='replace(local-name(), "[^\d]+", "")'>
          <xsl:apply-templates select="current-group()"/>
      </einschub>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="current-group()"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:for-each-group>     

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

> -----Original Message-----
> From: gregor FELLENZ [mailto:gregor@xxxxxxxxxxxx] 
> Sent: 21 April 2009 09:47
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject:  grouping with xsl:for-each-group
> 
> hi there,
> 
> i've got a flat xml document and need some hierachy in there. 
> the first approach was promising, but now i'm stuck how to 
> fix the last bit.
> i think i could do a procedural style, but i guess theres an 
> elegant solution for the problem.
> 
> i would like to put any group of "einschub" elements in a 
> separate container. this works fine when there's an other 
> element inbetween. but fails with different "einschub" 
> sibling-elements.
> 
> step by step:
> 1. source doc:
> <kapitel>
>      <abs>abs</abs>
>      <einschub1>einschub1</einschub1>
>      <einschub1>einschub1</einschub1>
>      <abs>abs</abs>
>      <einschub1>einschub1</einschub1>
>      <einschub2>einschub2</einschub2>
>      <abs>abs</abs>
>      <abs>abs</abs>
> </kapitel>
> 
> 2. xslt
> <xsl:template match="kapitel">
>      <xsl:element name="kapitel">
>          <xsl:for-each-group select="*" 
> group-adjacent="starts-with(local-name(),'einschub')" >
>              <xsl:choose>
>                  <xsl:when test="current-grouping-key()">
>                      <xsl:element name="einschub">
>                          <xsl:attribute name="typ" 
> select='replace(local-name(), "[^\d]+", "")'/>
>                          <xsl:apply-templates 
> select="current-group()"/>
>                      </xsl:element>
>                  </xsl:when>
>                  <xsl:otherwise>
>                      <xsl:apply-templates select="current-group()"/>
>                  </xsl:otherwise>
>              </xsl:choose>
>          </xsl:for-each-group>
>      </xsl:element>
> </xsl:template>
> 
> <xsl:template match="einschub1|einschub2">
>          <xsl:element name="abs">
>              <xsl:apply-templates/>
>          </xsl:element>
> </xsl:template>
> 
> 3. output with saxon9
> <kapitel>
>      <abs>abs</abs>
>      <einschub typ="1">
>          <abs>einschub1</abs>
>          <abs>einschub1</abs>
>      </einschub>
>      <abs>abs</abs>
>      <einschub typ="1">
>          <abs>einschub1</abs>
>          <abs>einschub2</abs>
>      </einschub>
>      <abs>abs</abs>
>      <abs>abs</abs>
> </kapitel>
> 
> 
> 4. desired output
> <kapitel>
>      <abs>abs</abs>
>      <einschub typ="1">
>          <abs>einschub1</abs>
>          <abs>einschub1</abs>
>      </einschub>
>      <abs>abs</abs>
>      <einschub typ="1">
>          <abs>einschub1</abs>
>      </einschub>
>      <einschub typ="2">
>          <abs>einschub2</abs>
>      </einschub>
>      <abs>abs</abs>
>      <abs>abs</abs>
> </kapitel>
> 
> any help is greatly appreciated!
> 
> 
> all the best,
> gregor

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.