Re: [xsl] template match attributes

Subject: Re: [xsl] template match attributes
From: "Bob DuCharme" <bob@xxxxxxxxx>
Date: Tue, 29 May 2001 14:55:18 -0400
> Is it possible to match attributes in a template match?
>
> <xsl:template match="//path/mypath/@type">

Sure. Just remember that when you match an attribute node of your source
tree, if you've copied its parent node to the result tree, you can't hang
just anything off the result tree version of the parent node. For example,
to transform this source document,

<path>
    <mypath type="blood" flavor="mint">
    The mypath content
    </mypath>
  </path>

the following stylesheet will replace the type attribute with a color="blue"
attribute value pair, but trying to add the empty test element to the same
place in the result tree wouldn't work:

  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="1.0">

  <xsl:template match="//path/mypath/@type">
    <xsl:attribute name="color">blue</xsl:attribute>
    <!-- this wouldn't work: <test/> -->
  </xsl:template>

  <!-- Copy all the other nodes -->
  <xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
  </xsl:template>

  </xsl:stylesheet>

(By the way, you probably don't need the "//" at then beginning of your
XPath expression, especially if path really is the root element.)

Bob DuCharme            www.snee.com/bob             <bob@
snee.com>      see http://www.snee.com/bob/xsltquickly for
info on upcoming "XSLT Quickly" from Manning Publications.



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


Current Thread