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

Re: Finding sequences of same element

Subject: Re: Finding sequences of same element
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Wed, 9 Feb 2005 20:32:13 +1100
xsl not self
On Wed, 9 Feb 2005 18:58:35 +1100, Simon Kissane <skissane@xxxxxxxxx> wrote:
> Hi
> 
> Suppose I have an input document:
> <A><B X="1"/><B X="2"/><B X="3"/><C X="4"/><B X="5"/><B X="6"/><B X="7"/></A>


Have you been using an obfuscation tool? Ever heard of formatting and
indentation?

Not that I'm trying to be picky or I'm in a bad mood, but producing
examples in the above "format" significantly decreases the number of
those, who could volunteer to decipher it (or as someone put it on his
blog, do we have to be masochists?).



> 
> Now, suppose I wish to group together consecutive B elements, giving a
> result document like this:
> <A><D><B X="1"/><B X="2"/><B X="3"/></D><C X="4"/><D><B X="5"/><B
> X="6"/><B X="7"/></D></A>
> 
> How can I do this? (I would prefer not to use recursive templates, but
> rather for-each, if that is at all possible...)
> 
> I think I can find the initial element of these sequences, like so:
>  B[not(preceding-sibling::*) or preceding-sibling::*[0][not(self::B)]]
> This, I think, should select all B for which there are either no
> preceeding sibling elements, or for which the immediately preceeding
> sibling element is not a B element. Thus, in the above example, it
> would pick B[@X=1] and B[@X=5].
> 
> But, given each initial sequence element, how can I find the remaining
> nodes in the sequence?  With the initial sequence element as the
> context node, I could do:
>    .|following-sibling::B
> But that will also pick up B[@X=5] and B[@X=6] when the context node is B[@X=1].
> 
> Is there a predicate test I could use on following-sibling::B to
> restrict it only to the current sequence of B elements?


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:strip-space elements="*"/>
 
 <xsl:key name="kNextGroup" match="B" 
   use="generate-id(following-sibling::*[not(self::B)][1])"/>
 
  <xsl:template match="node()|@*" name="identity">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="B[not(preceding-sibling::*[1][self::B])]">
    <D>
      <xsl:copy-of select=
       "key('kNextGroup', 
               generate-id(following-sibling::*[not(self::B)][1])
               )" />
    </D>
  </xsl:template>
  
  <xsl:template match="B"/>
</xsl:stylesheet>

when applied on your source xml document and its much more readable version:

<A>
  <B X="1"/>
  <B X="2"/>
  <B X="3"/>
  <C X="4"/>
  <B X="5"/>
  <B X="6"/>
  <B X="7"/>
</A>

produces the readable version of the wanted result:

<A>
   <D>
      <B X="1"/>
      <B X="2"/>
      <B X="3"/>
   </D>
   <C X="4"/>
   <D>
      <B X="5"/>
      <B X="6"/>
      <B X="7"/>
   </D>
</A>

Hope this helped.

Cheers,
Dimitre Novatchev.

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.