开发者

How to ignore namespaces during xsl translation time

I have an xml as below.

<?xml version="1.0" encoding="UTF-8"?>
<books xmlns="http://www.boo开发者_StackOverflow中文版ks.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.


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>


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜