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

RE: Transforming an XML document where the content isn

Subject: RE: Transforming an XML document where the content isn't in a special tag
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 17 Aug 2005 08:02:45 +0100
c recursion xml document
This is actually quite a tricky problem: if you want to read up on it,
search for "positional grouping". You are trying to group a sequence of
adjacent sibling nodes into a single <p> element. The thing that these nodes
have in common is that they are not <header> elements.

There must be a wrapper element in your XML for it to be well-formed, let's
suppose it is:

<doc>
<header>
First Section
</header>
This section deals with a lot of <a href="bla.htm">things</a>.
One of them, is...

<header>
Second Section
</header>
Now, this section is different, becase...
</doc>

In XSLT 2.0 you can do positional grouping using group-adjacent:

<xsl:template match="doc">
  <xsl:for-each-group select="node()"
group-adjacent="boolean(self::header)">
    <xsl:choose>
    <xsl:when test="self::header">
     <xsl:apply-templates select="current-group()"/>
    </xsl:when>
    <xsl:otherwise>
     <p><xsl:apply-templates select="current-group()"/>
    </xsl:otherwise>
    </xsl:c
  </xsl:
</xsl:

In XSLT 1.0 my preferred approach is what I call "sibling recursion":

<xsl:template match="doc">
  <xsl:for-each select="header">
    <h1><xsl:value-of select="."/></h1>
    <p><xsl:apply-templates select="following-sibling::node()[1]"
mode="across"/></p>
  </xsl:for-each>
</xsl:template>

<xsl:template match="node()" mode="across">
  <xsl:copy-of select="."/>
  <xsl:apply-templates select="following-sibling::node()[1]" mode="across"/>
</xsl:template>

<xsl:template match="header" mode="across">
  <!-- do nothing: terminate the recursion -->
</xsl:template>

>  I can, of course, add a
> </p> before a header and a <p> after it,

No, you can't do that. XSLT creates nodes, not tags. You create a P element
node on a tree, the serializer later turns that into a balanced pair of
tags, <P> and </P>

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

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.