0
votes

I have the following xml code:

<OML>    
  <bg-def xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="EX1"/>
</OML>

I want to remove the attribute xmlns:xsi and its value using XSLT, so that the result will look like this:

<OML>    
  <bg-def name="EX1"/>
</OML>

I tryied to do this with the following XSLT code:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
xmlns:ex="http://exslt.org/dates-and-times" extension-element-prefixes="ex">
 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"  xml:space="preserve"/>

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="bg-def|@ xmlns:xsi"/>
</xsl:transform>

Before I finished to write my code, my editor warned me that: "W Namespace prefix xmlns has not been declared". When I remove the expression :xsi and just write xmlns, there is no warning more. But when I compile and execute my program, nothing happens and I don't get the expected output. I try also to change the last line of my xslt file with this:

<xsl:template match="bg-def|@ name"/>

then the result is looking like this:

<OML>    
  <bg-def xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</OML>

That means, the attribute name has been removed very well. But I want to do this with the attribute xmlns:xsi. Can someone help me to do this please? Thanks for any help. Franky

2

2 Answers

0
votes

Use following template for bd-def node:

<xsl:template match="bg-def">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates select="node()|@*"/>
    </xsl:element>
</xsl:template>

Instead of

<xsl:template match="bg-def|@ name"/>

This template will create node bg-def and copy all it context nodes and attributes, but not namespaces

Check similar question: remove namespace for a perticular element

Update:

Source file:

<OML>    
     <bg-def xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="EX1"/>
</OML>

Stylesheet:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
    xmlns:ex="http://exslt.org/dates-and-times" extension-element-prefixes="ex">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no" xml:space="preserve"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="bg-def">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="node()|@*"/>
        </xsl:element>
    </xsl:template>

</xsl:transform>

Transformation result (Saxon 6.5.5 - Xslt 1.0):

<?xml version="1.0" encoding="UTF-8"?><OML>    
     <bg-def name="EX1"/>
</OML>
0
votes
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>

<xsl:template match="*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates/>
    </xsl:element>
</xsl:template>
<xsl:template match="@*">
    <xsl:attribute name="{local-name()}">
      <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>

</xsl:stylesheet>