How to ignore namespaces during xsl translation

I have xml as below.

<?xml version="1.0" encoding="UTF-8"?> <books xmlns="http://www.books.com/SRK"> <name>English</name> </books 

I needed the following output after translating using xsl.

 <?xml version="1.0" encoding="UTF-8"?> <books> <name>English</name> </books> 

I need xsl to ignore the namespace. I tried something but did not work with namespace.

I need your help. Your help will be appreciated.

+4
source share
2 answers

This conversion is :

 <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:template match="@*|node()[not(self::*)]"> <xsl:copy/> </xsl:template> <xsl:template match="*"> <xsl:element name="{local-name()}"> <xsl:apply-templates select="node()|@*"/> </xsl:element> </xsl:template> </xsl:stylesheet> 

when applied to the provided XML document :

 <books xmlns="http://www.books.com/SRK"> <name>English</name> </books> 

creates the desired, correct result :

 <books> <name>English</name> </books> 
+5
source

It only works if I have included the above templates, if I add other templates other than the above, then the translation does not work at all. None of the patterns are executed.

You are probably missing the namespace declaration for the book element. Example:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:b="http://www.books.com/SRK" exclude-result-prefixes="b"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <!-- @dimitre answer templates --> <xsl:template match="b:name"> <!-- your template for name --> </xsl:template> </xsl:stylesheet> 

Also, be sure to use the local-name() function to get the name of the element without the corresponding namespace.


Example

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:b="http://www.books.com/SRK" exclude-result-prefixes="b"> <xsl:output method="xml" encoding="UTF-8" indent="yes"/> <xsl:template match="b:input"> <xsl:element name="{local-name(.)}"> <xsl:apply-templates select="b:name"/> </xsl:element> </xsl:template> <xsl:template match="b:name"> <xsl:element name="{local-name()}"> <xsl:value-of select="."/> </xsl:element> <lhs> <xsl:apply-templates select="following-sibling::b:lhs/b:evaluate"/> </lhs> </xsl:template> <xsl:template match="b:evaluate"> Something to evaluate... </xsl:template> </xsl:stylesheet> 

gets:

 <?xml version="1.0" encoding="UTF-8"?> <input> <name>English</name> <lhs> Something to evaluate... </lhs> </input> 

Second example

You can create a separate transformation called local-identity.xsl containing the @Dimitre solution. Then you can import it into your conversion. Since you have a namespace to match the elements, you must change all of your XPaths, including the prefix that you specify in the conversion, as in the following example:

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:brl="http://www.xyz.com/BRL" exclude-result-prefixes="brl" version="1.0"> <xsl:import href="local-identity.xsl"/> <xsl:output method="xml" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="/brl:rule"> <!-- do your staff, select using brl prefix --> <xsl:apply-templates/> </xsl:template> </xsl:stylesheet> 
+2
source

All Articles