Namespace crawl when copying XML using XSLT

Starting with XML with the default namespace:

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

I use 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 get the following XML (it’s okay that β€œB” is not dumped because I use HTML as an output method):

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

But 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 deleted after the XSLT process.

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

+4
source share
1 answer

Not recommended, but works:

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

it is 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> 
+9
source

Source: https://habr.com/ru/post/1286068/


All Articles