[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] creating nested structure using the following stylesheet


Subject: [xsl] creating nested structure using the following stylesheet
From: Andreas Peter <info@xxxxxxxxxx>
Date: Thu, 22 Nov 2007 06:45:15 +0100

Hello List!

I need to create a nested structure from the following source:

<root>
    <element>
        <h1>h1</h1>
        <h2>h2</h2>
        <para>para</para>
        <para>para<emp>auszeichnung</emp></para>
        <para>para</para>
        <para>para</para>
        <para>para</para>
        <h2>h2</h2>
        <para>para</para>
        <h3>h3</h3>
        <para>para</para>
        <h5>h5</h5>
        <para>para</para>
        <para>para<emp>auszeichnung</emp></para>
        <para>para</para>
        <h4>h4</h4>
        <para>para</para>
        <para>para</para>
        <h2>h2</h2>
        <para>para</para>
        <para>para</para>
        <para>para</para>
        <h3>h3</h3>
        <para>para<emp>auszeichnung</emp></para>
        <para>para</para>
        <para>para</para>
        <h4>h4</h4>
        <para>para</para>
        <para>para</para>
        <h5>h5</h5>
        <para>para</para>
        <para>para<emp>auszeichnung</emp></para>
        <para>para</para>
    </element>
</root>

For output I use this stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
    <xsl:output indent="yes"/>
    <xsl:template match="/">
        <set>
            <book>
                <bookinfo/>
                <xsl:apply-templates/>
            </book>
        </set>
    </xsl:template>

    <xsl:template match="element">
        <xsl:apply-templates select="h1|h2[1]"/>
    </xsl:template>

    <xsl:template match="h1">
        <title>
            <xsl:apply-templates/>
        </title>
    </xsl:template>

    <xsl:template match="h2">
        <chapter>
            <title>
                <xsl:apply-templates/>
            </title>

            <xsl:for-each-group select=".|following-sibling::*"
group-starting-with="h2|h3|h4|h5">

                <xsl:variable name="para-content">
                    <xsl:copy-of select="current-group()[self::para]"/>
                </xsl:variable>

                <xsl:choose>
                    <xsl:when test="position()=1">
                        <xsl:copy-of select="current-group()[self::para]"/>
                    </xsl:when>

                    <xsl:when test="current-group()[name()='h2']">
                            <title>
                                <xsl:apply-templates/>
                            </title>
                            <xsl:copy-of select="$para-content"/>
                    </xsl:when>

                    <xsl:when test="current-group()[name()='h3']">
                        <xsl:element
name="sect{number(substring-after(name(.),'h'))-2}">
                            <title>
                                <xsl:apply-templates/>
                            </title>
                            <xsl:copy-of select="$para-content"/>
                        </xsl:element>
                    </xsl:when>

                    <xsl:when test="current-group()[name()='h4']">
                        <xsl:element
name="sect{number(substring-after(name(.),'h'))-2}">
                            <title>
                                <xsl:apply-templates/>
                            </title>
                            <xsl:copy-of select="$para-content"/>
                        </xsl:element>
                    </xsl:when>

                    <xsl:when test="current-group()[name()='h5']">
                        <xsl:element
name="sect{number(substring-after(name(.),'h'))-2}">
                            <title>
                                <xsl:apply-templates/>
                            </title>
                            <xsl:copy-of select="$para-content"/>
                        </xsl:element>
                    </xsl:when>
                </xsl:choose>
            </xsl:for-each-group>
        </chapter>
    </xsl:template>
</xsl:stylesheet>


Each sect2 should be child of sect1, sect3 should be child of sect2, sect3 should be child of sect4 and sect4 should be child of sect5. Is this possible with the stylesheet above or do I need to rethink my approach?

A second question: the emp element inside a para will not match when I use

<xsl:template match="emp">
 <emphasis role="bold">
  <xsl:apply-templates/>
 </emphasis>
</xsl:template>

Is this because para is inside a xsl:for-each-group?

Thanks for helping me,
Andreas


Current Thread