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

creating nested structure using the following styleshe

Subject: creating nested structure using the following stylesheet
From: Andreas Peter <info@xxxxxxxxxx>
Date: Thu, 22 Nov 2007 06:45:15 +0100
 creating nested structure using the following styleshe
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

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.