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

Re: Suggestions to improve my XSL writing style

Subject: Re: Suggestions to improve my XSL writing style
From: Brandon Ibach <brandon.ibach@xxxxxxxxxxxxxxxxxxx>
Date: Fri, 28 Jan 2011 07:05:48 -0500
Re:  Suggestions to improve my XSL writing style
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

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.