tags:

views:

235

answers:

1

Starting from an XML with a default namespace:

<Root>
  <A>foo</A>
  <B></B>
  <C>bar</C>
</Root>

I apply an XSLT to remove the 'C' element:

<?xml version="1.0" ?>

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

<xsl:output method="html" indent="no" encoding="utf-8" />

<xsl:template match="*">
        <xsl:copy>
                <xsl:copy-of select="@*" />
                <xsl:apply-templates />
        </xsl:copy>
</xsl:template>

<xsl:template match="C" />

</xsl:stylesheet>

and I end up with the following XML (it's OK to have 'B' not collapsed because I'm using HTML as output method):

<Root>
  <A>foo</A>
  <B></B>
</Root>

But then if I ever get another XML, this time with a namespace:

<Root xmlns="http://company.com"&gt;
  <A>foo</A>
  <B></B>
  <C>bar</C>
</Root>

the 'C' element is not removed after XSLT process.

What can I do to bypass this namespace, is there a way?

+8  A: 

Not so recommendable, but works:

<xsl:template match="*[local-name()='C']" />

Better:

<xsl:stylesheet 
  version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:foo="http://company.com"
  exclude-result-prefixes="foo"
>

  <!-- ... -->

  <xsl:template match="C | foo:C" />

  <!-- ... -->

</xsl:stylesheet>
Tomalak
Great! Thanks a lot, it works :-)
Tiago Fernandez
I'd debate the "not so recommendable" part, but I don't want to start that whole thing up again :)
annakata
@annakata: I know. :-) But I am okay with the first approach. I'm just saying the second one is cleaner. And performs better, probably. ;-)
Tomalak