tags:

views:

47

answers:

1

I am new in xslt. I have the following problem. I need within an xml, to remove a specific attribute from a specific element. i.e.

<html>
   <head>...</head>
   <body>
      <div id="qaz" theAtribute="44">
       <div>
       <div id ="ddd" theAtribute="4">
           <div id= "ggg" theAtribute="9">
           <div>
       <div>
    </body>
</html>

to become

<html>
   <head>...</head>
   <body>
      <div id="qaz">
       <div>
       <div id ="ddd">
           <div id= "ggg">
           <div>
       <div>
    </body>
</html>

Where attribute theAtribute has been removed. I found this, http://www.biglist.com/lists/xsl-list/archives/200404/msg00668.html based on which i made attempts to find the proper solution.

i.e. <xsl:template match="@theAtribute" />

Which removed it from the whole document... and others like match, if choose, etc. Nothing worked.. :-( can you please help me on this? it sound trivial to me, but with xslt, i cannot cope at all...

Thank you all in advance

+2  A: 

What is not working? Do you want the same content, just without the @theAtribute?

If so, make sure your stylesheet has the empty template for @theAtribute, but also has an identity template that copies everything else into the output:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <!--empty template suppresses this attribute-->
    <xsl:template match="@theAtribute" />
    <!--identity template copies everything forward by default-->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

If you only want to suppress certain @theAtribute, then you can make the match criteria more specific. For instance, if you only wanted to remove that attribute from the div who's @id="qaz", then you could use this template:

<xsl:template match="@theAtribute[../@id='qaz']" />

If you want to remove @theAttribute from all div elements, then change the match expression to:

<xsl:template match="div/@theAtribute" />
Mads Hansen
Hello, these two i had found as well i am afraid but they are not helpful. the first one removes the "theAtribute" from everywhere (divs, body, font, etc tags). The second approach removes from everywhere the @theAtribute from all nodes with id=qaz. Is there a way for me to write <xsl:template match="@theAtribute[../@nodename='div']" />??what i need, is a mix of these two i think, but i cannot figure out the format due to mi ignorance on xslt.I which to remove the theAtribute from all divs and only from divs.
Alexandros
I see. I didn't realize in the description that you specifically wanted to exclude `div/@theAtribute`. I've updated the answer. If your XML elements are bound to a namespace, you may need to declare the namespace in your XSLT, or use an XPATH statement that ignores the namespace and matches on the local-name (i.e. `*[local-name()='div']/@theAtribute`)
Mads Hansen
It worked!!!! perfect solution and really fast response. Thank you Hansen!!!!
Alexandros