xsl-list
[Top] [All Lists]

[xsl] creating nested structure using the following stylesheet

2007-11-21 22:45:57
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


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>
  • [xsl] creating nested structure using the following stylesheet, Andreas Peter <=