Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore namespaces during xsl translation time

Tags:

xslt

I have an xml as below.

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

I required the following output after translation using xsl .

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

I need an xsl to ignore the namespace.I have tried something but its not working with namespace.

I need your help.Your help would be appreciated.

like image 731
malledugu Avatar asked Jun 06 '11 14:06

malledugu


People also ask

What is exclude result prefixes in XSLT?

The optional exclude-result-prefixes attribute is a list, delimited by white-space, of the namespaces prefixes that should not be copied into the output (the result tree).

What is xsl namespace?

XSLT Elements The namespace name http://www.w3.org/1999/XSL/Transform is used primarily to identify elements which serve as declarations or instructions in the XSLT language.

Is xsl otherwise mandatory?

The xsl:otherwise element is an optional child of the xsl:choose element. The xsl:choose element is used to make a choice when there are two or more possible courses of action.

How do I debug an xsl template?

Debug the Transformation On the XML Transform diagram, right-click on the XML Transform Activity element and select the 'Debug XSL Transformation' option. The XSLT Debugger view displays, showing the stylesheet (. xsl) file and XML document used in the transformation, which is automatically initiated.


2 Answers

This transformation:

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

produces the wanted, correct result:

<books>
   <name>English</name>
</books>
like image 147
Dimitre Novatchev Avatar answered Sep 22 '22 00:09

Dimitre Novatchev


Its working only if i include the above templates if i add some other templates other than the above one then the translation is not at all working .None of the template getting executed.

Probably you are missing the declaration of the namespace 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's answer templates -->

    <xsl:template match="b:name">
          <!-- your template for name -->
    </xsl:template>

</xsl:stylesheet>

Moreover, make sure to use local-name() function to get the name of an element without the related 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 transform called local-identity.xsl containing @Dimitre solution. Then you can import it in your transform. Because you have a namespace, to match elements you must change all your XPaths including the prefix you will declare in the transform, 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>
like image 29
Emiliano Poggi Avatar answered Sep 23 '22 00:09

Emiliano Poggi