Parsing XML with unknown namespaces in Oracle SQL

前端 未结 1 1450
旧时难觅i
旧时难觅i 2021-02-06 07:58

I\'m having trouble with Oracle SQL and XMLs.

I\'ll be getting loads of clobs of well-formed XML data from an external system to parse, interpret and fill some tables wi

相关标签:
1条回答
  • 2021-02-06 08:16

    I know this is pretty old, but I spotted it today and remembered the pain I experienced trying to deal with namespaced XML. My solution was to strip out the namespaces with an XSLT transform and process it as plain old XML. The function I used to do this is:

    function remove_namespace( i_xml in xmltype )
      return xmltype
    is
      v_xml xmltype default i_xml;
      v_xsl varchar2(32767);
    begin
      v_xsl := '<?xml version="1.0" encoding="UTF-8"?>
            <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:template match="*">
              <!-- remove element prefix (if any) -->
              <xsl:element name="{local-name()}">
              <!-- process attributes -->
              <xsl:for-each select="@*">
                <!-- remove attribute prefix (if any) -->
                <!-- this if filters out any xmlns="" atts that have no
                     namespace prefix in the xml -->
                <xsl:if test="(local-name() != ''xmlns'')">
                  <xsl:attribute name="{local-name()}">
                    <xsl:value-of select="."/>
                  </xsl:attribute>
                </xsl:if>
              </xsl:for-each>
             <xsl:apply-templates/>
             </xsl:element>
             </xsl:template>
             </xsl:stylesheet>';
      return v_xml.transform(xmltype(v_xsl));
    end;
    
    0 讨论(0)
提交回复
热议问题