[xsl] Muenchian Method Doesn't go far enough for me

Subject: [xsl] Muenchian Method Doesn't go far enough for me
From: "Richard Lander" <rlander@xxxxxxxxxxxxx>
Date: Wed, 16 Oct 2002 17:15:09 -0700
 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