24

I need to copy from input document to output document all attributes but one.

My input is like this:

<mylink id="nextButton" type="next" href="javascript:;" />

And I need output like this:

<a id="nextButton" href="javascript:;" />

If I use the following XSL:

<xsl:template match="mylink">
    <a><xsl:copy-of select="attribute::*"/></a>
</xsl:template>

I get all attributes to output like this:

<a id="nextButton" type="next" href="javascript:;" />

But I want to ignore the "type" attribute. I've tried the following but none of them seems to work the way I need:

<xsl:copy-of select="attribute::!type"/>
<xsl:copy-of select="attribute::!'type'"/>
<xsl:copy-of select="attribute::*[!type]"/>
<xsl:copy-of select="attribute::not(type)"/>

How should I write my stylesheet to get needed output?

Dimitre Novatchev
  • 235,605
  • 26
  • 291
  • 421
martsraits
  • 2,825
  • 2
  • 26
  • 24

2 Answers2

41

Shortest form:

<xsl:template match="mylink">
    <a><xsl:copy-of select="@*[name()!='type']"/></a>
</xsl:template>

Longer one (that's the first thing I came up with, I leave it for reference):

<xsl:template match="mylink">
    <a>
     <xsl:for-each select="@*">
      <xsl:if test="name() != 'type'">
       <xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
      </xsl:if> 
     </xsl:for-each>
    </a>
</xsl:template>
vartec
  • 125,354
  • 36
  • 211
  • 241
  • I used the longer one to swap out the name of an attribute ("change `type` to `class`"). Is there a version of the shorter one that will accomplish the same thing? – Max Heiber Sep 22 '16 at 18:56
2

In XSLT 2.0:

<xsl:template match="mylink">
  <a><xsl:copy-of select="@* except @type"/></a>
</xsl:template>
yegor256
  • 97,508
  • 114
  • 426
  • 573