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

Siblings to hierarchy: Better way to do this?

Subject: Siblings to hierarchy: Better way to do this?
From: Richard Bondi <rbondi@xxxxxxxxxx>
Date: Fri, 06 Sep 2002 11:19:26 -0400
xsl stylesheets and siblings
Dear All,

I need to transform stuff in this form:

<r>
        <section/>
        <paragraph>adolf</paragraph>
        <paragraph>alex</paragraph>
        <section/>
        <paragraph>bertram</paragraph>
        <paragraph>bilbo</paragraph>
        <paragraph>bumble</paragraph>
        <section/>
        <paragraph>claus</paragraph>
        <paragraph>charles</paragraph>
        <paragraph>cymbaline</paragraph>
</r>

to this:

<r>
        <section>
                <Para>adolf</Para>
                <Para>alex</Para>
        </section>
        <section>
                <Para>bertram</Para>
                <Para>bilbo</Para>
                <Para>bumble</Para>
        </section>
        <section>
                <Para>claus</Para>
                <Para>charles</Para>
                <Para>cymbaline</Para>
        </section>
</r>

The style-sheet which worked seems clumsy to me:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<r>
<xsl:apply-templates select="/r/section"/>
</r>
</xsl:template>
<xsl:template match="section">
<xsl:variable name="gid" select="generate-id()"/>
<section>
<xsl:apply-templates select="following-sibling::paragraph[generate-id(preceding-sibling::section[1])=$gid]"/>
</section>
</xsl:template>
<xsl:template match="paragraph">
<Para>
<xsl:apply-templates/>
</Para>
</xsl:template>
</xsl:stylesheet>


Can anyone suggest something faster and/or more elegant please?

Also: I don't understand why this didn't work: if anyone can explain,
I'd be grateful:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<r>
<xsl:apply-templates select="/r/section"/>
</r>
</xsl:template>
<xsl:template match="section">
<xsl:variable name="sect" select="self::node()"/>
<section>
<!-- match only paragraphs whose first preceding sibling is the section we are processing -->
<xsl:apply-templates select="following-sibling::paragraph[preceding-sibling::section[1]=$sect]"/>
</section>
</xsl:template>
<xsl:template match="paragraph">
<Para>
<xsl:apply-templates/>
</Para>
</xsl:template>
</xsl:stylesheet>


Thank you,
Richard Bondi



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.