Re: [xsl] Remove a particluar Attribute from all the node

Subject: Re: [xsl] Remove a particluar Attribute from all the node
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 22 Apr 2004 09:56:40 +0100
Hi Animesh,

> I want to write an XSL which will remove the particular attribute
> from all the node in the DOM tree.

Since you want the tree to basically stay the same, the core of your
transformation should be the identity template:

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

On its own, this creates a deep copy of the source document. If you
want to exclude a particular attribute, you just need to add a
template that matches that attribute and does nothing with it (doesn't
copy it). So, to exclude the fr_elementid attribute, you should couple
the identity template above with the template:

<xsl:template match="@fr_elementid" />

By the way, in your XSLT you had:

> <xsl:template match="@*">
> 	 <xsl:choose>
> 	 <xsl:when test="name()='fr_elementid'">	
> 	 </xsl:when>
> 	<xsl:otherwise>
> 		<xsl:copy>
> 			<xsl:apply-templates select="."/>
> 		</xsl:copy>	
> 	</xsl:otherwise>
> 	</xsl:choose>
> </xsl:template>

When this matches an attribute other than the fr_elementid attribute,
it copies that attribute and then applies templates to that attribute.
The template that gets used on the attribute is this one, so the
attribute gets copied again and templates applied to it. And so it
goes on and on in an infinite loop. When you copy an attribute, you
get its value as well, so what you actually wanted was:

  <xsl:otherwise>
    <xsl:copy />
  </xsl:otherwise>

or, if you prefer:

  <xsl:otherwise>
    <xsl:copy-of select="." />
  </xsl:otherwise>

Cheers,

Jeni

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

Current Thread