RE: [xsl] Changing an attribute wherever it may occur

Subject: RE: [xsl] Changing an attribute wherever it may occur
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 27 Sep 2001 16:17:12 -0400
Matt--

Do just what he said, only qualify the match by whatever your criterial actually are.

So, Chris had (modified slightly for conciseness):

<xsl:template match="node()|@*">
  <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="@Action">
  <xsl:attribute name="Action">M</xsl:attribute>
</xsl:template>

But you want

to do it for the <A> elements that met a
certain criteria, such as @Id='X', as opposed to the whole document?

So change that second template to


<xsl:template match="A[@Id='X']//@Action">
  <xsl:attribute name="Action">M</xsl:attribute>
</xsl:template>

or just

<xsl:template match="A[@Id='X']//@Action">
  <xsl:copy>M</xsl:copy>
</xsl:template>

Any Action attribute nodes anywhere inside an A whose Id='X', will be changed to have value "M".

To explicate further, '//' is short for /descendant-or-self::node()/, so you are saying you want to match on any Action attribute attached to any A with Id='X', or attached to any descendant of such an A. It will fail to match Action attributes that are not attached to, or descendants of, such an A.

This is a good example of how compact and elegant XSLT can get when you understand both the power of template matching and applying, and how to construct XPath expressions that match (or select) just what you want.

Cheers,
Wendell



======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


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



Current Thread