Re: [xsl] How to strip off all <xsd:annotation> ...</xsd.annotation> tags

Subject: Re: [xsl] How to strip off all <xsd:annotation> ...</xsd.annotation> tags
From: Michael Ludwig <mlu@xxxxxxxxxxxxx>
Date: Thu, 13 Aug 2009 16:52:17 +0200
Martin Honnen schrieb:
Ben Stover wrote:

thank you for your suggestion. Unfortunately your script show the
following error (when running with Saxon):

  XTDE0410: An attribute node (targetNamespace) cannot be created
  after the children of the containing element at xsl:apply-templates
  (file:/D:/xslt/Saxon/strip%20annotation%20tags/stripannotationtags.xsl#11)
     processing /xsd:schema/@targetNamespace
  in built-in template rule

Do you know what's wrong?

I can't explain that error. And I have now tried the stylesheet against the schema http://www.w3.org/2001/xml.xsd and it processes that without errors. Can you provide a minimal but complete XML input document that causes that error?

Ben, please keep things like this on list instead of sending mail privately. Your stylesheet is not the one Martin posted; you introduced a significant change:

<xsl:stylesheet xml:space="preserve"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
   xmlns:xsd="http://www.w3.org/2001/XMLSchema";
   version="2.0">

<xsl:template match="xsd:annotation"/>

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

</xsl:stylesheet>

The xml:space on the xsl:stylesheet is a very bad idea. It leads to
every single whitespace-only text node being relevant, which makes the
stylesheet create whitespace-only text nodes after the <xsl:template>
and after <xsl:copy> (and elsewhere). So child nodes are created
*before* applying templates to attributes. Creating attributes after
having created child nodes is not allowed.

Just to illustrate the principle here, the following will work (but it's
a very odd arrangement, avoid that):

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


Just get rid of the xml:space="preserve" - it's the source of your
problems. Sometimes you'll want whitespace-only text nodes in your
source tree (XSLT stylesheet module) to be kept and copied, but then
you'll probably also want to localize the effect of the xml:space you
insert to achieve that effect.

--
Michael Ludwig

Current Thread