sbo
sbo

Reputation: 971

xsl match template by attribute except one

I've a global match on an attribut in my stylesheet but I want to exclude the f - element. How can I do that?

Example XML:

<a>
<b formatter="std">...</b>
<c formatter="abc">...</c>
<d formatter="xxx">
    <e formatter="uuu">...</e>
    <f formatter="iii">
        <g formatter="ooo">...</g>
        <h formatter="uuu">...</h>
    </f>
</d>
</a>

Current solution:

<xsl:template match="//*[@formatter]">
   ...
</xsl:template>

I've tried something like this, but that didn't worked.

<xsl:template match="f//*[@formatter]">
...
</xsl:template>

<xsl:template match="//f*[@formatter]">
...
</xsl:template>

Upvotes: 0

Views: 202

Answers (1)

JLRishe
JLRishe

Reputation: 101748

Either //f[@formatter] or f[@formatter] would have worked (the // is not necessary). When this XSLT is run on your example input:

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

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

  <xsl:template match="*[@formatter]">
    <xsl:element name="transformed-{local-name()}">
      <xsl:apply-templates select="@* | node()" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="f[@formatter]">
    <xsl:apply-templates select="node()" />
  </xsl:template>
</xsl:stylesheet>

The result is:

<a>
  <transformed-b formatter="std">...</transformed-b>
  <transformed-c formatter="abc">...</transformed-c>
  <transformed-d formatter="xxx">
    <transformed-e formatter="uuu">...</transformed-e>

      <transformed-g formatter="ooo">...</transformed-g>
      <transformed-h formatter="uuu">...</transformed-h>

  </transformed-d>
</a>

As you can see, the f is excluded. Does this answer your issue, or have I misunderstood what you want to do?

Upvotes: 3

Related Questions