[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Removing an XML Attribute

Subject: Re: Removing an XML Attribute
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 05 Jul 2007 11:58:14 -0400
Re:  Removing an XML Attribute
Hi,

At 07:11 PM 7/4/2007, was written:
On Wed, Jul 04 2007 19:10:22 +0100, Shaun.McDonald@xxxxxxxxxxx wrote:
> I came up with something that appears to work, only I'm not sure if
> there is a caveat to doing it this way.
>
> Since I'm transforming almost 100 files, I want to make sure nothing
> bites me in the rear.
>
> Using XSLT 2.0, Saxon 8.8
>
> <xsl:template match="*">
>   <xsl:copy>
>     <xsl:choose>
>      <xsl:when test="not(@change)">
>        <xsl:copy-of select="@* "/>
>      </xsl:when>
>      <xsl:otherwise/>
>    </xsl:choose>
>    <xsl:apply-templates/>
>  </xsl:copy>
> </xsl:template>
>
> I'm still spot-checking the files, but so far it seems to have removed
> the @change attribute only, which is my desired output.

That would appear to be because @change is the only attribute on that
particular element.

Your template copies all attributes if @change is not present, and
copies no attributes if it is present.

(If you have just one test, you can use xsl:if instead of xsl:choose,
and when you do use xsl:choose, the xsl:otherwise is optional.)

Try just:

<xsl:copy-of select="@*[not(self::change)]"/>

I'm afraid this isn't going to work, on account of attribute elements not appearing on the self:: axis. (A small but significant wrinkle.)


You might have to work around just a bit more:

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

<xsl:template match="@*">
  <xsl:copy-of select="."/>
</xsl:template>

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

When you apply templates to the attributes, only the @change attributes will match the empty template, and will hence be dropped. Other attributes will match the "next best" template matching attributes, which copies them.

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 ======================================================================

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2011 All Rights Reserved.