How to rename an Oracle XMLTYPE node

前端 未结 2 1372
北荒
北荒 2021-01-13 14:59

I have an XMLType in PL/SQL and I need to rename some of the nodes and some of the values. For example:


   
        foo&l         


        
相关标签:
2条回答
  • 2021-01-13 15:50

    Or, you could define an xsl stylesheet that will make the change and use dbms_xslprocessor to do your modifications.

    0 讨论(0)
  • 2021-01-13 15:52

    One option is to use XMLTRANSFORM to rename a node. See also e.g. Rename nodes with XSLT.

    with
    xmldata as (select xmltype('<root>
      <fields>
        <a>foo</a>
        <b>bar</b>
      </fields>
    </root>') val from dual),
    stylesheet as (select '<?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <!-- Identity transformation -->
      <xsl:template match="node()|@*">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
      </xsl:template>
      <!-- Identity transformation overridden for element b -->
      <xsl:template match="b">
        <xsl:element name="c">
          <xsl:apply-templates select="node()|@*"/>
        </xsl:element>
      </xsl:template>
    </xsl:stylesheet>' val from dual)
    select xmltransform(x.val, s.val) from xmldata x, stylesheet s;
    

    Output:

    XMLTRANSFORM(X.VAL,S.VAL)
    --------------------------------------------------------------------------------
    <root>
      <fields>
        <a>foo</a>
        <c>bar</c>
      </fields>
    </root>
    
    0 讨论(0)
提交回复
热议问题