Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 980439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:22:52+00:00 2026-05-16T04:22:52+00:00

I created an XSLT stylesheet which looks for a node and deletes it. This

  • 0

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

  <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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:22:53+00:00Added an answer on May 16, 2026 at 4:22 am

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a xslt document which formats an xml document, but I would
I want to create conditional comments in XSLT. But when I use this: <!--
I have an XSLT 1.0 (2.0 is not an option) stylesheet which produces XHTML.
I'm having trouble obtaining data with a macro I've created using XSLT. I have
I have an XSLT in which I create (from the input data) intermediary variables
Created .NET WCF service, tested it - works. Generated schemas from Data and service
I've created a menu in umbraco using XSLT. The menu is using the usual
I've generated the following XSLT file, and have created a Form that will post
I'm trying to query an xml file using the following xslt: <xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform
I need to have my XSLT stylesheet sort my XML file's child nodes, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.