xsl-list
[Top] [All Lists]

RE: xslt 2.0: grouping flat element structures to make lists

2005-05-26 01:35:18
Thanks for the solution David Carlisle, this certainly does the
flat-to-grouped job I need. 

However, one more query (open to anyone): Would I need to add this grouping
processing approach to ALL the possible parent elements that the flat-list
structures can occur in? 

In my DTD there are many parent element possibilities and what's more many
already have a fair amount of complex code in their templates (don't want to
upset this if I can avoid it). That's why I tried to use the first <par
class="Listbegin"> as a kind of trigger for the list processing. Is this
route not the one to take due to XSLT processing model?

Goal (<Any-element> is a wildcard and not necessarily the root):

<?xml version="1.0" encoding="UTF-8"?>
<Any-element>
 <a>some text</a>
 <par class="Listbegin">Fruit</par>
 <par class="Listitem>Apple</par>
 <par class="Listitem>Orange</par>
 <par class="Listitem>Pear</par>
 <a>more text</a>
 <par class="Listbegin">Colours</par>
 <par class="Listitem>Red</par>
 <par class="Listitem>Green</par>
 <par class="Listitem>Blue</par>
 <par class="Listbegin">Shapes</par>
 <par class="Listitem>Triangle</par>
 <par class="Listitem>Circle</par>
 <par class="Listitem>Square</par>
 <a>yet more text</a>
</Any-element>

to produce

<?xml version="1.0" encoding="UTF-8"?>
<data>
 <a>some text</a>
 <list>
  <head>Fruit</head>
  <item>Apple</item>
  <item>Orange</item>
  <item>Pear</item>
 </list>
 <a>more text</a>
 <list>
  <head>Colours</head>
  <item>Red</item>
  <item>Green</item>
  <item>Blue</item>
 </list>
<list>
  <head>Shapes</head>
  <item>Triangle</item>
  <item>Circle</item>
  <item>Square</item>
 </list>
 <a>yet more text</a>
</data> 

Thanks

Derek Revill


-----Original Message-----
From: David Carlisle [mailto:davidc(_at_)nag(_dot_)co(_dot_)uk]
Sent: 25 May 2005 22:30
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] xslt 2.0: grouping flat element structures to make
lists



<sect>

<foo/>
<xyz/>
<xyz/>
<par class="Listbegin">Fruit</par>
<par class="Listitem">Apple</par>
<par class="Listitem">Orange</par>
<par class="Listitem">Pear</par>
<par class="Listbegin">Colours</par>
<par class="Listitem">Red</par>
<par class="Listitem">Green</par>
<par class="Listitem">Blue</par>
<xyz/>
<bar/>
<par class="Listbegin">Fruit</par>
<par class="Listitem">Apple</par>
<par class="Listitem">Orange</par>
<par class="Listitem">Pear</par>
</sect>



<xsl:stylesheet
   version="2.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";



<xsl:output indent="yes"/>

<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="sect">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:for-each-group group-adjacent="@class='Listitem'" select="*">
<xsl:choose>
<xsl:when test="self::par[(_at_)class='Listitem']">
<list>
<head><xsl:apply-templates
select="preceding-sibling::par[1]/node()"/></head>
<xsl:apply-templates select="current-group()"/>
</list>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>

<xsl:template match="par[(_at_)class='Listbegin']"/>


</xsl:stylesheet>



$ saxon8 list.xml list.xsl
<?xml version="1.0" encoding="UTF-8"?>
<sect>
   <foo/>
   <xyz/>
   <xyz/>
   <list>
      <head>Fruit</head>
      <par class="Listitem">Apple</par>
      <par class="Listitem">Orange</par>
      <par class="Listitem">Pear</par>
   </list>
   <list>
      <head>Colours</head>
      <par class="Listitem">Red</par>
      <par class="Listitem">Green</par>
      <par class="Listitem">Blue</par>
   </list>
   <xyz/>
   <bar/>
   <list>
      <head>Fruit</head>
      <par class="Listitem">Apple</par>
      <par class="Listitem">Orange</par>
      <par class="Listitem">Pear</par>
   </list>
</sect>

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

--~------------------------------------------------------------------
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>
--~--


--~------------------------------------------------------------------
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>
--~--