xsl-list
[Top] [All Lists]

Re: [xsl] PARAMS and VARIABLES in XSL

2007-01-30 13:05:39

I don't think you need any variables, despite the subject line,


<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:strip-space elements="clause"/>
<xsl:output indent="yes"/>

<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="word">
 <word soundtime="{((.|preceding-sibling::sound)/@time)[last()]}">
   <xsl:apply-templates/>
 </word>
</xsl:template>

<xsl:template match="sound"/>

</xsl:stylesheet>

to comment on your code


<xsl:template match="//clause/*">

there's never a need to start a match pattern with // it doesn't chage
which nodes are matched (not quite true in xslt)

<xsl:if test="local-name() = 'sound'">

generally it's better to avoid doing string tests against element names

<xsl:if test="self::sound">

is likely to be more efficient 9and safer in a namespace context)

<xsl:if test="local-name() = 'sound'">
<xsl:param name="soundtime" select="@time" />
</xsl:if>


xsl:param can only be used at the top level as a template, where they
must come first, or for global parameters must be at the top levl of the
styleseet. You could use xsl:variable there but the scope of a variable
binding is to the end of the containing element, so the variable would
go out of scope at the </xsl:if>

In more complicated case you do need to use a parameter but in that case
you need to apply templates in a different order, instead of applying
templates to all the children, just apply templates to the first child,
then have each child apply templates to the next sibling, passing on a
paramater with xsl:with-param. This is usually known as a tree-walking
idiom, google should show some examples, but in this case you can
directly evaluate the required value on each node, so there is no need
for a parameter to accumulate earlier results.

David

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--