[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: template match attributes
> 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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|