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">
<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>