Start of change

Example: Using XSLT to remove namespaces

XML documents you receive might contain unneeded or incorrect namespace information. You can use XSLT style sheets to remove or manipulate the namespace information in the documents.

The following examples show how to use XSLT to remove namespace information from an XML document. The examples store the XML document and the XSLT stylesheets in XML columns and use the XSLTRANSFORM function to convert the XML document using one of the XSLT stylesheets.

The following CREATE statements create the tables XMLDATA and XMLTRANS. XMLDATA contains a sample XML document, and XMLTRANS contains XSLT stylesheets.

CREATE TABLE XMLDATA (ID BIGINT NOT NULL PRIMARY KEY, XMLDOC XML );
CREATE TABLE XMLTRANS (XSLID BIGINT NOT NULL PRIMARY KEY, XSLT XML );

Add the sample XML document to the XMLDATA table using the following INSERT statement.

insert into XMLDATA (ID, XMLDOC) values ( 1, '
<newinfo xmlns="http://mycompany.com">
<!-- merged customer information -->
  <customerinfo xmlns="http://oldcompany.com" xmlns:d="http://test" Cid="1004">
     <name>Matt Foreman</name>
  <addr country="Canada">
     <street>1596 Baseline</street>
     <city>Toronto</city>
     <prov-state>Ontario</prov-state>
     <pcode-zip>M3Z 5H9</pcode-zip>
  </addr >
  <phone type="work" >905-555-4789</phone>
  <h:phone xmlns:h="http://test1" type="home">416-555-3376</h:phone>
  <d:assistant>
      <name>Gopher Runner</name>
      <h:phone xmlns:h="http://test1" type="home">416-555-3426</h:phone>
  </d:assistant>
  </customerinfo>
</newinfo>
');

Example XSLT stylesheet that removes all namespaces

The following example uses an XSLT stylesheet to remove all namespace information from the XML document stored in the table XMLDATA. The examples stores the stylesheet in the table XMLTRANS and uses a SELECT statement to apply the stylesheet to the XML document.

Add the stylesheet to the XMLTRANS table using the INSERT statement.

insert into XMLTRANS (XSLID, XSLT) values ( 1, '
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  
 <!-- keep comments -->
 <xsl:template match="comment()">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
    <!-- remove element prefix -->
    <xsl:element name="{local-name()}">
      <!-- process attributes -->
      <xsl:for-each select="@*">
        <!-- remove attribute prefix -->
        <xsl:attribute name="{local-name()}">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:for-each>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>
') ;

The following SELECT statement converts the sample XML document using the XSLT stylesheet.

SELECT XSLTRANSFORM (XMLDOC USING XSLT ) 
  FROM XMLDATA, XMLTRANS 
  where ID = 1 and XSLID = 1

The XSLTRANSFORM command converts the XML document using the first XSLT stylesheet and returns the following XML with all the namespace information removed.

<?xml version="1.0" encoding="UTF-8"?>
<newinfo>
	<!-- merged customer information -->
	<customerinfo Cid="1004">
		<name>Matt Foreman</name>
		<addr country="Canada">
			<street>1596 Baseline</street>
			<city>Toronto</city>
			<prov-state>Ontario</prov-state>
			<pcode-zip>M3Z 5H9</pcode-zip>
		</addr>
		<phone type="work">905-555-4789</phone>
		<phone type="home">416-555-3376</phone>
		<assistant>
			<name>Gopher Runner</name>
			<phone type="home">416-555-3426</phone>
		</assistant>
	</customerinfo>
</newinfo>

Example XSLT stylesheet that keeps the namespace binding for an element

The following example uses an XSLT stylesheet keeps the namespace binding for only the phone elements. The name of the element is specified in the XSLT variable mynode. The example stores the stylesheet in the table XMLTRANS and uses a SELECT statement to apply the stylesheet to the XML document.

Add the stylesheet to the XMLTRANS table using the following INSERT statement.

insert into XMLTRANS (XSLID, XSLT) values ( 2, '
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:variable name ="mynode">phone</xsl:variable>
  
  <!-- keep comments -->
  <xsl:template match="comment()">
      <xsl:copy>
        <xsl:apply-templates/>
      </xsl:copy>
   </xsl:template>
   
  <xsl:template xmlns:d="http://test" xmlns:h="http://test1" match="*">
  <xsl:choose>
   
  <!-- keep namespace prefix for node names $mynode -->
    <xsl:when test="local-name() = $mynode " >
     <xsl:element name="{name()}">
       <!-- process node attributes -->
       <xsl:for-each select="@*">
       <!-- remove attribute prefix  -->
         <xsl:attribute name="{local-name()}">
           <xsl:value-of select="."/>
         </xsl:attribute>
       </xsl:for-each>
      <xsl:apply-templates/>
     </xsl:element>
    </xsl:when>
      
  <!-- remove namespace prefix from node -->
    <xsl:otherwise>
     <xsl:element name="{local-name()}">
       <!-- process node attributes -->
       <xsl:for-each select="@*">
       <!-- remove attribute prefix  -->
         <xsl:attribute name="{local-name()}">
           <xsl:value-of select="."/>
         </xsl:attribute>
       </xsl:for-each>
      <xsl:apply-templates/>
     </xsl:element>
     </xsl:otherwise>
  </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
');

The following SELECT statement converts the sample XML document using the second XSLT stylesheet since XSLID = 2 is specified.

SELECT XSLTRANSFORM (XMLDOC USING XSLT) 
  FROM XMLDATA, XMLTRANS 
  where ID = 1 and XSLID = 2 ;

The XSLTRANSFORM command converts the XML document using the second XSLT stylesheet and returns the following XML with the namespaces for only the phone elements.

<?xml version="1.0" encoding="UTF-8"?>
<newinfo>
	<!-- merged customer information -->
	<customerinfo Cid="1004">
		<name>Matt Foreman</name>
		<addr country="Canada">
			<street>1596 Baseline</street>
			<city>Toronto</city>
			<prov-state>Ontario</prov-state>
			<pcode-zip>M3Z 5H9</pcode-zip>
		</addr>
		<phone type="work">905-555-4789</phone>
		<h:phone xmlns:h="http://test1" type="home">
			416-555-3376
		</h:phone>
		<assistant>
			<name>Gopher Runner</name>
			<h:phone xmlns:h="http://test1" type="home">
				416-555-3426
			</h:phone>
		</assistant>
	</customerinfo>
</newinfo>
End of change