xsl-list
[Top] [All Lists]

Re: How do I remove an attribute from a file?

2004-05-28 13:07:39
At 2004-05-28 12:24 -0700, Jon Steeves wrote:
I have an xml file that contains a tag called <bit>. <bit> has two attribute: type and status, so it often looks like this:

<bit type="a" status="b">bitinfo</bit>

I want to remove all status attributes when they appear (they are optional), so that the result will look like this:

<bit type="a">bitinfo</bit>

Can anyone help me?

If you want to use XSLT to do this, then use an identity transformation where you prescribe that the status attribute of bit elements adds nothing in the construction of the result tree, while everything else reconstructs itself in the result tree.

An example is below.

Note, however, you may be incurring a capacity and performance hit by using XSLT for such a simple transform ... you may wish to use a program with a SAX interface instead because of a smaller footprint and faster execution.

I hope this helps.

........................... Ken

T:\ftemp>type jon.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<bits>
  <bit type="a" status="b">bitinfo</bit>
  <bit type="b" status="c">bitinfo</bit>
  <bit type="d">bitinfo</bit>
  <bit status="e">bitinfo</bit>
  <bit type="f" status="g">bitinfo</bit>
</bits>
T:\ftemp>type jon.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:template match="bit/@status"/><!--add nothing to the result for these-->

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

</xsl:stylesheet>
T:\ftemp>saxon jon.xml jon.xsl
<?xml version="1.0" encoding="utf-8"?><bits>
  <bit type="a">bitinfo</bit>
  <bit type="b">bitinfo</bit>
  <bit type="d">bitinfo</bit>
  <bit>bitinfo</bit>
  <bit type="f">bitinfo</bit>
</bits>
T:\ftemp>


--
Public courses: Spring 2004 world tour of hands-on XSL instruction
Next: 3-day XSLT/XPath; 2-day XSL-FO - Birmingham, UK June 14,2004

World-wide on-site corporate, govt. & user group XML/XSL training.
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Breast Cancer Awareness  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal



<Prev in Thread] Current Thread [Next in Thread>