RE: [xsl] Changing Attribute Value in all the ChildNodes at any level down the current node.

Subject: RE: [xsl] Changing Attribute Value in all the ChildNodes at any level down the current node.
From: Mukul Gandhi <mukul_gandhi@xxxxxxxxx>
Date: Fri, 28 May 2004 09:49:39 -0700 (PDT)
Hi Animesh,
  Please try the XSL -

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" version="1.0"
encoding="UTF-8" indent="yes"/>

<xsl:template match="node()">
   <xsl:copy>	      	 
     <xsl:if test="ancestor::csf">	      	     
       <xsl:apply-templates select="@*[not(name() =
'href')]"  mode="x"/>
       <xsl:apply-templates select="@*[name() =
'href']"  mode="y"/>
     </xsl:if>
     <xsl:if test="not(ancestor::csf)">
       <xsl:apply-templates select="@*"  mode="x"/>
     </xsl:if>
     <xsl:apply-templates />	               
   </xsl:copy>
</xsl:template>
	
<xsl:template match="@*" mode="x">
  <xsl:attribute name="{name()}">
     <xsl:value-of select="." />
  </xsl:attribute>
</xsl:template>
	
<xsl:template match="@*" mode="y">
   <!-- blank template -->
</xsl:template>
		
</xsl:stylesheet>

This removes the attribure "href" from the subtree of
node "csf".

For e.g., when the XSL is applied to XML -

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <body>
    <namespace>
      <form>
	<snip href="1" a="2" b="3">
	  <csf href="4" c="5" d="6">
	    <td href="7" e="8" f="9">			      <table
href="10" g="11" h="12">
                table1
              </table>
	    </td>
	  </csf>
	</snip>
      </form>
    </namespace>
  </body>
  <body>
    <namespace>
      <form>
	<snip href="1" a="2" b="3">
	  <csf href="4" c="5" d="6">
	    <td href="7" e="8" f="9">			      <table
href="10" g="11" h="12">
                table2
              </table>
	    </td>
	  </csf>
	</snip>
      </form>
    </namespace>
  </body>
</root>

it produces output ,

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <body>
    <namespace>
      <form>
	<snip href="1" a="2" b="3">
	  <csf href="4" c="5" d="6">
	    <td e="8" f="9">
              <table g="11" h="12">
                table1
              </table>
	    </td>
	  </csf>
	</snip>
      </form>
    </namespace>
  </body>
  <body>
    <namespace>
      <form>
	<snip href="1" a="2" b="3">
	  <csf href="4" c="5" d="6">
	    <td e="8" f="9">
              <table g="11" h="12">
                table2
              </table>
	    </td>
	  </csf>
	</snip>
      </form>
    </namespace>
  </body>
</root>

Regards,
Mukul

--- Animesh Sharma <asharma@xxxxxxxxxxxxxxxx> wrote:
> 
> Thanks for the suggestion. 
> 
> But the problem is I wanted to retain the other part
> of the original XML. It is something that I want to
> modify the property (Attribute) of sub tree while
> not touching the rest of Tree.
> 
> Something like:
> 
> <xsl:template
>
match="//body/namespace/form/snip/csf/td[position()=1]/table[position()=2]">
> 	<xsl:copy>
> 		<xsl:apply-templates
> select="@*[not(name()='href')]"/>
> 		<xsl:apply-templates select="*|text()"/>
> 	</xsl:copy>
> </xsl:template>
> 
> 'Default Template
> <xsl:template match="*|text()|@*">
> 	<xsl:copy>
> 		<xsl:apply-templates select="*|text()|@*"/>
> 	</xsl:copy>
> </xsl:template>
> 
> And this thing doesn't works. Don't know why? 
> May be Default template might reintroduce the
> attribute removed by first template. 
> 
> Will changing priority of first template would be of
> any help?
> 
> Thanks once again
> Animesh
> 
> 
> -----Original Message-----
> From: Andreas L. Delmelle
> [mailto:a_l.delmelle@xxxxxxxxxx]
> Sent: Thursday, May 27, 2004 9:40 PM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: RE: [xsl] Changing Attribute Value in all
> the ChildNodes at any
> level down the current node.
> 
> 
> > -----Original Message-----
> > From: Animesh Sharma
> [mailto:asharma@xxxxxxxxxxxxxxxx]
> >
> 
> Hi,
> 
> > I have to remove a particular attribute (in
> following example it
> > is "href") from all the ChildNodes of the
> particular node.
> >
> > I tried to write template something as follows:
> >
> > <xsl:template
> >
>
match="//body/namespace/form/snip/csf/td[position()=1]/table[posit
> > ion()=2]">
> > 	<xsl:apply-templates select="@*"/>
> > 	<xsl:attribute name="href"/>
> 
> AFAIK, xsl:attribute is not a self-closing element,
> so if you really want to
> create an empty href attribute, at least there
> should be
> <xsl:attribute name="href"></xsl:attribute>
> 
> Although, I'm not sure whether this will work...
> (haven't needed or tested
> anything like it)
> 
> If you just want to omit the href attribute from
> being output, you have two
> options:
> 
> For both: remove the 'empty' xsl:attribute element
> you specified above. It
> serves no purpose.
> 
> 1. replace <xsl:apply-templates select="@*" /> by
>    <xsl:apply-templates
> select="@*[not(name()='href')]" />
> 
> 2. keep the current form of the apply-templates
> instruction, and define a
> null template matching @href, like <xsl:template
> match="@href" />
> 
> 
> Hope this helps!
> 
> Greetz,
> 
> Andreas
> 



	
		
__________________________________
Do you Yahoo!?
Friends.  Fun.  Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/ 

Current Thread