Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bypassing namespaces while copying an XML with XSLT

Tags:

java

xml

xslt

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

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

like image 740
Tiago Fernandez Avatar asked Jan 24 '23 12:01

Tiago Fernandez


1 Answers

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>
like image 102
Tomalak Avatar answered Jan 30 '23 09:01

Tomalak