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

Muenchian Method Doesn't go far enough for me

Subject: Muenchian Method Doesn't go far enough for me
From: "Richard Lander" <rlander@xxxxxxxxxxxxx>
Date: Wed, 16 Oct 2002 17:15:09 -0700
xslt sectioning
 Afternoon,

I'm having some trouble with grouping.

I've moved to the Muenchian method, which makes good sense to me.

A similar problem to mine would be writing a transform to convert flat HTML sections to hiearchical sections.

In HTML, you might have:

H1
H2
H2
H3
H2
H1
H2

Let's remap it to:
<test>
<section>
	<title>H1</title>
</section>
<section>
	<title>H2</title>
</section>
<section>
	<title>H2</title>
</section>
<section>
	<title>H3</title>
</section>
<section>
	<title>H2</title>
</section>
<section>
	<title>H1</title>
</section>
<section>
	<title>H2</title>
</section>
</test>

You would want to convert to the following sectioning structure:

<test>
<section>
	<title>H1</title>
	<section>
		<title>H2</title>
	</section>
	<section>
		<title>H2</title>
		<section>
			<title>H3</title>
		</section
	</section>
	<section>
		<title>H2</title>
	</section
</section>
<section>
	<title>H1</title>
	<section>
		<title>H2</title>
	</section>
</section>
</test>

I've mapped my current solution, which I'd like to improve on, to this problem set. Here goes:

C:\temp\XSLTProject2->
- type XSLTProject2_data.xml
<test>
        <section>
                <title>H1</title>
        </section>
        <section>
                <title>H2</title>
        </section>
        <section>
                <title>H2</title>
        </section>
        <section>
                <title>H3</title>
        </section>
        <section>
                <title>H2</title>
        </section>
        <section>
                <title>H1</title>
        </section>
        <section>
                <title>H2</title>
        </section>
</test>

C:\temp\XSLTProject2->
- type XSLTProject2.xslt
<?xml version="1.0"?>
<xslt:transform xmlns:xslt="http://www.w3.org/1999/XSL/Transform" version="1.0">


        <xslt:key name="sections" match="section" use="title"/>

        <xslt:template match="/">
                <xslt:apply-templates/>
        </xslt:template>

        <xslt:template match="test">
                <test>
                        <xslt:apply-templates select="key('sections','H1')"/>
                </test>
        </xslt:template>

        <xslt:template match="section">
                <xslt:variable name="baseName" select="'H'"/>
                <xslt:variable name="currentHeading" select="title"/>
                <xslt:variable name="childHeading">
                        <xslt:value-of select="$baseName"/>
                        <xslt:value-of select="substring-after(title,$baseName) + 1"/>
                </xslt:variable>


                <section>
                        <title><xslt:value-of select="title"/></title>

                        <xslt:apply-templates select="key('sections',$childHeading)[generate-id(current()) = generate-id(preceding-sibling::section[title = $currentHeading][1])]"/>

                </section>
        </xslt:template>


</xslt:transform>

C:\temp\XSLTProject2->
- type out.xml
<?xml version="1.0" encoding="utf-8" ?>
<test>
        <section>
                <title>H1</title>
                <section>
                        <title>H2</title>
                </section>
                <section>
                        <title>H2</title>
                        <section>
                                <title>H3</title>
                        </section>
                </section>
                <section>
                        <title>H2</title>
                </section>
        </section>
        <section>
                <title>H1</title>
                <section>
                        <title>H2</title>
                </section>
        </section>
</test>

Anyway, so my transform works ... However, I'm sure that the following isn't very efficient:

<xslt:apply-templates select="key('sections',$childHeading)[generate-id(current()) = generate-id(preceding-sibling::section[title = $currentHeading][1])]"/>

Is there anyway to improve on this and still achieve the same result tree?

Thanks,

Rich

 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.