Re: [xsl] Suggestions to improve my XSL writing style

Subject: Re: [xsl] Suggestions to improve my XSL writing style
From: Brandon Ibach <brandon.ibach@xxxxxxxxxxxxxxxxxxx>
Date: Fri, 28 Jan 2011 07:05:48 -0500
I'll reiterate others' comments about syntax of empty elements and how
you can almost never have too many <xsl:apply-templates/>
instructions, with one caveat.  XSLT automatically includes several
default templates, including:

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

This makes your template for "speaker" unnecessary, as the default
template will cover it, but it doesn't hurt to be explicit, either.

For the numbering, try adding this inside <xsl:template match="scene">:

<xsl:for-each select="parent::act">Act <xsl:number/>, </xsl:for-each>

-Brandon :)


On Fri, Jan 28, 2011 at 3:20 AM, Jacobus Reyneke
<jacobusreyneke@xxxxxxxxx> wrote:
> Good day,
>
> The XSL I wrote below works, but I'm looking for ways to improve my
> coding style. Is this a bad way of writing style sheets? - in
> particular the fact that I call "apply-templates" about a million
> times.
>
> Also, I could not get the Act numbering to be added in the Scene
> title, so I left it in the Act element, where I would have liked a
> Scene title like "Act 1, Scene 1" but using position() from within the
> scene match gave me strange numbering.
>
> Input XML:
> <?xml version="1.0" encoding="UTF-8"?>
> <play>
>  <title>Tragedy of MACBETH</title>
>  <act>
>  <scene>
>   <stage-direction>An open place. Thunder and lightning. Enter three
> Witches</stage-direction>
>   <speech><speaker>1ST WITCH</speaker>
>   <line>When shall we three meet again</line>
>   <line>In thunder, lightning, or in rain?</line>
>   </speech><speech><speaker>2ND WITCH</speaker>
>   <line>When the hurly-burly's done</line>
>   <line>When the battle's lost and won.</line>
>   </speech><speech><speaker>3RD WITCH</speaker>
>   <line>That will be ere the set of sun.</line>
>   </speech><speech><speaker>1ST WITCH</speaker>
>   <line>Where the place?</line>
>   </speech><speech><speaker>2ND WITCH</speaker>
>   <line>Upon the heath.</line>
>   </speech><speech><speaker>3RD WITCH</speaker>
>   <line>There to meet with Macbeth. <stage-action>Cat
> meows.</stage-action></line>
>   </speech><speech><speaker>1ST WITCH</speaker>
>   <line>I'm coming, Graymalkin. <stage-action>Toad
> croaks.</stage-action></line>
>   </speech><speech><speaker>2ND WITCH</speaker>
>   <line><stage-direction-in-dialogue>to Third
> Witch</stage-direction-in-dialogue> Toad's
>    calling.</line>
>   </speech><speech><speaker>3RD WITCH</speaker>
>   <line>I'm coming now.</line>
>   </speech><speech><speaker>ALL</speaker>
>   <line>Fair is foul, and foul is fair;</line>
>   <line>Hover through the fog and filthy air. <stage-action>They
> vanish.</stage-action></line>
>  </speech></scene>
> </scene>
> </act>
> </play>
>
> XSLT:
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>    xmlns:xs="http://www.w3.org/2001/XMLSchema";
> xmlns:mbp="http://www.amazon.com/amazonything";
> exclude-result-prefixes="xs" version="2.0">
>    <xsl:template match="play">
>        <html>
>            <body>
>            <xsl:apply-templates></xsl:apply-templates>
>            </body>
>        </html>
>    </xsl:template>
>    <xsl:template match="title">
>        <h1><xsl:apply-templates></xsl:apply-templates></h1>
>    </xsl:template>
>    <xsl:template match="act">
>        <mbp:pagebreak />
>        <h1>Act <xsl:number></xsl:number></h1>
>        <xsl:apply-templates></xsl:apply-templates>
>    </xsl:template>
>    <xsl:template match="scene">
>        <mbp:section>
>        <h2>Scene <xsl:number></xsl:number></h2>
>        <xsl:apply-templates></xsl:apply-templates>
>            </mbp:section>
>    </xsl:template>
>    <xsl:template match="stage-direction">
>        <hr/>
>        <p><em>
>        <xsl:apply-templates></xsl:apply-templates>
>        </em></p>
>        <hr/>
>    </xsl:template>
>    <xsl:template match="speech">
>        <p width="-40pt" text-indent="-40pt">
>            <xsl:apply-templates></xsl:apply-templates>
>        </p>
>    </xsl:template>
>    <xsl:template match="speaker">
>        <xsl:apply-templates></xsl:apply-templates>
>    </xsl:template>
>    <xsl:template match="line">
>        <xsl:text>
> </xsl:text><xsl:apply-templates></xsl:apply-templates><br/>
>    </xsl:template>
>    <xsl:template match="stage-action">
>        ---
>         <xsl:apply-templates></xsl:apply-templates>
>    </xsl:template>
>  </xsl:stylesheet>

Current Thread