如何使用REGEXP_REPLACE

时间:2019-02-08 11:18:26

标签: regex xml oracle plsql regexp-replace

我下面有这样的string / xml标记。请参见下面的代码...

<filterCriteriaRow>
  <filterCriteriaItem>
    <attributeName>abcd</attributeName>
    <columnName>SEGMENT3</columnName>
    <operator>EQUALTO</operator>
    <conjunction>OR</conjunction>
    <valueDataType>STRING</valueDataType>
    <value>000000</value>
  </filterCriteriaItem>
  <filterCriteriaItem>
    <attributeName>abcd</attributeName>
    <columnName>SEGMENT3</columnName>
    <operator>BETWEEN</operator>
    <conjunction>OR</conjunction>
    <valueDataType>STRING</valueDataType>
    <value>101001</value>
    <value>503099</value>
  </filterCriteriaItem>
  <filterCriteriaItem>
    <attributeName>abcd</attributeName>
    <columnName>SEGMENT3</columnName>
    <operator>EQUALTO</operator>
    <conjunction>OR</conjunction>
    <valueDataType>STRING</valueDataType>
    <value>000000</value>
  </filterCriteriaItem>
  <filterCriteriaItem>
    <attributeName>abcd</attributeName>
    <columnName>SEGMENT3</columnName>
    <operator>BETWEEN</operator>
    <conjunction>OR</conjunction>
    <valueDataType>STRING</valueDataType>
    <value>101001</value>
    <value>503099</value>
  </filterCriteriaItem>
  <conjunction>AND</conjunction>
</filterCriteriaRow>

我想操纵该字符串使其变为:

<filterCriteriaRow>
  <filterCriteriaItem>
    <attributeName>abcd</attributeName>
    <columnName>SEGMENT3</columnName>
    <operator>EQUALTO</operator>
    <conjunction>OR</conjunction>
    <valueDataType>STRING</valueDataType>
    <value>000000</value>
  </filterCriteriaItem>
  <filterCriteriaItem>
    <attributeName>abcd</attributeName>
    <columnName>SEGMENT3</columnName>
    <operator>BETWEEN</operator>
    <conjunction>OR</conjunction>
    <valueDataType>STRING</valueDataType>
    <value>101001</value>
    <value2>503099</value2>
  </filterCriteriaItem>
  <filterCriteriaItem>
    <attributeName>abcd</attributeName>
    <columnName>SEGMENT3</columnName>
    <operator>EQUALTO</operator>
    <conjunction>OR</conjunction>
    <valueDataType>STRING</valueDataType>
    <value>000000</value>
  </filterCriteriaItem>
  <filterCriteriaItem>
    <attributeName>abcd</attributeName>
    <columnName>SEGMENT3</columnName>
    <operator>BETWEEN</operator>
    <conjunction>OR</conjunction>
    <valueDataType>STRING</valueDataType>
    <value>101001</value>
    <value2>503099</value2>
  </filterCriteriaItem>
  <conjunction>AND</conjunction>
</filterCriteriaRow>

如您所见,一个父标记中有一个双标记列“值” 我要创建第一个值标签为“ value”,但第二个值标签为“ value2”

如何使用regexp_replace操作该字符串?

请帮助

1 个答案:

答案 0 :(得分:0)

Oracle中有许多XML选项,其中许多在最新版本中已弃用。

当前的一种选择是使用xquery更新,以下示例假定您的XML位于存储为XMLTYPE的表中。

假设每个“ filterCriteriaItem”有两个“值”节点...

SELECT XMLQUERY (
          'copy $copy := . 
           modify (
             for $fci in $copy/filterCriteriaRow/filterCriteriaItem/value[2]
                return rename node $fci as "value2")
           return $copy'
          PASSING xml_doc 
          RETURNING CONTENT) new_xml_doc
FROM   table_name;

或更一般地说...

SELECT XMLQUERY (
         'copy $copy := . 
          modify (
            for $fci in $copy/filterCriteriaRow/filterCriteriaItem
               for $val at $pos in $fci/value
                  return rename node $val as 
                     fn:concat("value", if ($pos = 1) then "" else $pos))
          return $copy'
         PASSING xml_doc 
         RETURNING CONTENT) new_xml_doc
FROM   table_name;