Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 21st, 2005, 10:51 AM
Registered User
 
Join Date: Dec 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Turning Siblings into Children

Hi,

Imagine that I have the following XML document:

<article>
    <heading level="1">Heading1</heading>
    <heading level="2">Sub1</heading>
    <paragraph>1st <bold>block</bold> of info</paragraph>
    <heading level="1">Heading2</heading>
    <heading level="2">Sub2</heading>
    <paragraph>2nd <italic>block</italic> of info</paragraph>
    <heading level="1">Heading3</heading>
    <heading level="2">Sub3</heading>
    <paragraph>3rd <strong>block</strong> of info</paragraph>
</article>

and I want to transform it into :


<article>
    <section>
        <heading>Heading1</heading>
        <sub-heading>Sub1</sub-heading>
        <paragraph>1st <bold>block</bold> of info</paragraph>
    </section>
    <section>
        <heading>Heading2</heading>
        <sub-heading>Sub2</sub-heading>
        <paragraph>2nd <italic>block</italic> of info</paragraph>
    </section>
    <section>
        <heading>Heading3</heading>
        <sub-heading>Sub3</sub-heading>
        <paragraph>3rd <strong>block</strong> of info</paragraph>
    </section>
</article>

I am using the following XSL :

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/article">
    <article>
        <xsl:apply-templates select="heading" />
    </article>
</xsl:template>

<xsl:template match="heading">
    <xsl:choose select=".">
        <xsl:when test ="@level &lt; 2">
            <xsl:element name="section">
                <xsl:element name="heading">
                    <xsl:value-of select="." />
                </xsl:element>
                <xsl:apply-templates select="following-sibling::*" />
            </xsl:element>
        </xsl:when>
        <xsl:otherwise>
            <xsl:element name="sub-heading">
                    <xsl:value-of select="." />
            </xsl:element>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

Unfortunately, it is not producing the desired result because I need it to stop at each "heading".

Does anyone have any ideas about how I can achieve my goal (without resorting to writing parsing code).

Thanks,

Simon

 
Old December 21st, 2005, 11:17 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The problem is known as "positional grouping" and you'll find examples if you google for that. In 2.0 it's done using

<xsl:for-each-group group-starting-with="pattern">

which you need to apply at each level of heading.

In 1.0 the two common techniques are

(a) reduce it to a value-based grouping problem, where the grouping key is generate-id(preceding-sibling::heading[@level='1'][1]) - you can then use Muenchian grouping using this key

(b) do "sibling recursion" where you process the siblings using <xsl:apply-templates select="following-sibling::*[1]"> - I'll leave you to find examples of that in the various FAQs and tutorials.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old December 22nd, 2005, 07:50 AM
Registered User
 
Join Date: Dec 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael,

Thanks for your help. I feel that I am a lot closer now using the following XSL:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="paras" match="paragraph | link | heading[@level=2]"
         use="preceding-sibling::heading[1]/text()" />

<xsl:template match="article">
  <article>
    <xsl:apply-templates select="heading[@level=1]" />
  </article>
</xsl:template>

<xsl:template match="heading[@level=1]">
  <xsl:element name="section">
      <xsl:copy-of select="." />
         <xsl:copy-of select="key('paras', text())" />
  </xsl:element>
</xsl:template>

</xsl:stylesheet>


However, I have a problem with "level 2 headers". Using the following XML with all of the level 2 headers commented out, all is well. However, as soon as the first level 2 header ("sub 1") is uncommented, the first section processes nothing past the first level 2 header and ONLY the paragraph element is processed in the other sections. When I replaced the level 2 headers with the element "subheading" that was fine (although I cannot do this in the final solution).

The text.xml file is :

<article>
    <heading level="1">Heading1</heading>

    <paragraph>1st <bold>block</bold> of info</paragraph>
    <paragraph>Sneaky</paragraph>
    <link>Points somewhere</link>
    <heading level="1">Heading2</heading>

    <paragraph>2nd <italic>block</italic> of info</paragraph>
    <heading level="1">Heading3</heading>

    <paragraph>3rd <strong>block</strong> of info</paragraph>
</article>

Any help would be appreciated.

Thanks,

Simon

 
Old December 22nd, 2005, 08:33 AM
Registered User
 
Join Date: Dec 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ooooops. Found the error (must remember to check code more thoroughly before posting replies).
The offending line has been altered from:

   use="preceding-sibling::heading[1]/text()" />

to:

   use="preceding-sibling::heading[@level=1][1]/text()"

See below:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="paras" match="paragraph | link | heading[@level=2]"
         use="preceding-sibling::heading[@level=1][1]/text()" />

<xsl:template match="article">
  <article>
    <xsl:apply-templates select="heading[@level=1]" />
  </article>
</xsl:template>

<xsl:template match="heading[@level=1]">
  <xsl:element name="section">
      <xsl:copy-of select="." />
         <xsl:copy-of select="key('paras', text())" />
  </xsl:element>
</xsl:template>

</xsl:stylesheet>

Thanks for your help.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Introducing parent node to specific siblings Frimann XSLT 11 July 3rd, 2008 03:20 PM
first element in array nesting siblings rjonk XSLT 1 May 18th, 2008 03:53 PM
Problem accessing the following siblings. Tre XSLT 6 June 5th, 2007 11:13 AM
Comparing siblings Chamkaur XSLT 1 June 17th, 2006 09:56 PM
Accessing Attributes of Siblings AForgue XSLT 2 November 4th, 2003 01:39 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.