This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Var references in "match" attribute of xsl:template?


David,

> I vaguely remember I've read that the value of the "match" attribute
> of "xsl:template" cannot reference a variable in any way.  If that's
> true, then I have to duplicate the domain class name in the
> "domainClass" setting, and in the top-level "xsl:template" tag.  It
> would be nice if I could define the domain class in a single place,
> and reference it in all the places it is used.  Is there any way to do
> this?

It's true that you cannot use a variable within the match pattern of a
template.  However, you can get around it in two ways.  The first is
to include the variable in whatever way you need to within the select
expression that is used to decide which nodes to apply templates to:

  <xsl:apply-templates select="*[name() = $domainClass]" />

and have a general template:

<xsl:template match="*">
   ...
</xsl:template>
  
The second is to embed conditional processing within a more general
template:

<xsl:template match="*">
  <xsl:if test="name() = $domainClass">
     ...
  </xsl:if>
</xsl:template>

Of these, the first is generally more efficient because it narrows
down the nodes that have templates applied to them earlier in the
process.

However, I note from your description that:

> The name of the root tag element is exactly the same as the domain
> class.

By 'root tag element' I guess you mean the document element: the
element at the very top of the document tree.  If that's the case,
then you can always get the name of that element through the XPath:

  name(/*) [or local-name(/*) if you're using namespaces]

i.e. the name of the element that is a child of the root node.  You
*can* use this expression within the match pattern of a template.

You can also match on the document element (the element representing
your domain class) with the match pattern:

  /*

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]