views:

71

answers:

1

I created an XSLT stylesheet which looks for a node and deletes it. This works great. I now want to check to see if a certain node exist and then delete that node if it is there.

So I attempted to add an if-statement and that's were I ran into the following error:

compilation error: file dt.xls line 10 element template
element template only allowed as child of stylesheet

I think I understand the error but not sure how to get around it.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

  <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="Ad">
    <xsl:template match="node()|@*">

      <xsl:if test="name-ad-size">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
      </xsl:if>

    </xsl:template>
  </xsl:template>


  <xsl:template match="phy-ad-width"/>
  <xsl:strip-space elements="*"/>
  <xsl:preserve-space elements="codeListing sampleOutput"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
+4  A: 

The problem, usually, when people first attempt XSLT is that they think it is a language as any other, like C#, Java, PHP. All these languages are used to tell the computer what to do. But with XSLT it is the reverse, you tell the processor what output you expect based on rules.

Sometimes, the use of xsl:if is good. More often, it is a sign of a mistake. The trick to delete nodes, elements, or text is to create a matching template that outputs nothing. Something like this:

<!-- starting point -->
<xsl:template match="/">
    <xsl:apply-templates select="root/something" />
</xsl:template>

<xsl:template match="name-ad-size">
   <!-- don't do anything, continue processing the rest of the document -->
   <xsl:apply-templates select="node() | @*" />
</xsl:template>

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

Why does this work? Simply, because the processor goes through each element and node and first looks at the best matching template. The best match for a node <name-ad-size> is the match that doesn't output anything, thus it effectively deletes it. Other nodes don't match, and so end up in the "catch all" template.

Note 1: the error you receive is likely because you have mistakenly added <xsl:template> inside another element. It can only be placed under the root <xsl:stylesheet> and nowhere else.

Note 2: the order of <xsl:template> statements is irrelevant. The processor will use all of them to find the best match, regardless where they're put (as long as they're directly under the root).


EDIT: Someone magically retrieved your code. Here's the story above applied to your complete stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:preserve-space elements="codeListing sampleOutput"/>

  <!-- NOTE: it is better to have a starting point explicitly -->
  <xsl:template match="/">
    <xsl:apply-templates select="root/something" />
  </xsl:template>

  <!-- I assume now that you meant to delete the <Ad> elements -->
  <xsl:template match="Ad">
     <xsl:apply-templates select="node()|@*"/>
  </xsl:template>

  <!-- NOTE: here you were already deleting <phy-ad-width> and everything underneath it -->
  <xsl:template match="phy-ad-width"/>

  <!-- NOTE: copies everything that has no matching rule elsewhere -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
Abel
The key to learning XSL is that it's not an imperative programming language. The XSL processor doesn't use the stylesheet to drive the processor; instead it's the input XML document (the one you're processing, not the stylesheet) that controls the process. As each tag is read, the processor decides what to do by searching the XSL for the best matching template. That's somewhat over-simplified, but learning to think of the document in control, not the stylesheet, is the required intuitive leap.
Jim Garrison
@Jim: I think that's what I was trying to say. I know of this reversed thinking and it is entirely correct, but I've noticed that pupils find it hard to think of the XML (or other input) being leading. Which is why I use a comparison with "rules". When I work with XSLT, I write rules, when I write rules, I ask *"if the processor comes across a node in the input XML matching my rule, what should we output?"*.
Abel
wow thanks I see what your saying and your right I'm a c# programmer and i was looking at it like that. however, before I delete the<xsl:template match="phy-ad-width"/> i want to check to see if the node "name-ad-size" exist. the reason I need this check is sometimes I need the phy-ad-width and sometimes I don't my key is the "name-ad size" if that exist then I want to delete the phy-ad-width this is why I thought a if statment might be in order here. thanks again for this fine answer.
Mike
@kc2scy: forget that, don't think in terms of *"if it is there or if it isn't"*. Create a rule for if it is there. It will be ignored when it isn't. If you must create a rule for when it isn't, use `<xsl:template match="parent-name[not(phy-ad-width)]">` which will be applied on a `<parent-name>` without a `<phy-ad-width>`. Many other rules or combinations are of course possible.
Abel