XML 仅在文本存在时查找并删除

XML Find and Delete only if text is present

因此在 M$SQL 2014 企业数据库中使用以下内容:

DECLARE @table TABLE (XmlCol XML)
INSERT INTO @table (XmlCol) VALUES ('
<DEFeatureClassInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:typens="http://www.esri.com/schemas/ArcGIS/10.8" xsi:type="typens:DEFeatureClassInfo">
  <GPFieldInfoExs xsi:type="typens:ArrayOfGPFieldInfoEx">
    <GPFieldInfoEx xsi:type="typens:GPFieldInfoEx">
      <Name>UNITCODE</Name>
      <AliasName>UNITCODE</AliasName>
      <ModelName>UNITCODE</ModelName>
      <FieldType>esriFieldTypeString</FieldType>
      <IsNullable>true</IsNullable>
      <Required>true</Required>
    </GPFieldInfoEx>
    <GPFieldInfoEx xsi:type="typens:GPFieldInfoEx">
      <Name>REGIONCODE</Name>
      <AliasName>REGIONCODE</AliasName>
      <ModelName>REGIONCODE</ModelName>
      <DomainName>DOM_REGIONCODE_NPS2016</DomainName>
      <FieldType>esriFieldTypeString</FieldType>
      <DefaultValueString>SER</DefaultValueString>
      <IsNullable>true</IsNullable>
      <Required>true</Required>
    </GPFieldInfoEx>
    <GPFieldInfoEx xsi:type="typens:GPFieldInfoEx">
      <Name>CREATEUSER</Name>
      <AliasName>CREATEUSER</AliasName>
      <ModelName>CREATEUSER</ModelName>
      <FieldType>esriFieldTypeString</FieldType>
      <DefaultValueString>GRSM User</DefaultValueString>
      <IsNullable>true</IsNullable>
      <Required>true</Required>
    </GPFieldInfoEx>
  </GPFieldInfoExs>
 </DEFeatureClassInfo>')

我想根据条件删除一个(元素?节点?)。在我粘贴的示例中,我有 3 个元素都带有

<Required>true</Required>

我想做的是删除

<Required>true</Required>

仅当

<Name>CREATEUSER</Name>

存在于同一个元素中(我在这里可能混淆了节点和元素)。

换句话说,这是一个 XML 列,它为 table 定义了数据库架构,我有数百个 table 需要修改 [=40] =] 模式定义,以便任何时候 CREATEUSER 出现,真正的被删除(不改变或替换)。

到目前为止,我最多

SELECT *
FROM   GDB_ITEMS
WHERE  GDB_ITEMS.Definition.exist(N'//Name[text()="CREATEUSER"]') = 1;

这花了我 4 个小时的谷歌搜索,并至少向我展示了我数据库中的哪些 table 在 XML 模式定义中有 CREATEUSER。只需要触摸所有这些并删除 true。需要澄清的是,在其他情况下,我 不想 删除 true。在上面发布的同一示例中,我们看到 true 出现在 "UNITCODE" 节点中,而 true 不能是 deleted/removed。我只想删除与 CREATEUSER 关联的 true。

请尝试以下操作。

SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (XmlCol XML)
INSERT INTO @tbl (XmlCol) VALUES 
(N'<DEFeatureClassInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xmlns:xs="http://www.w3.org/2001/XMLSchema"
                    xmlns:typens="http://www.esri.com/schemas/ArcGIS/10.8"
                    xsi:type="typens:DEFeatureClassInfo">
    <GPFieldInfoExs xsi:type="typens:ArrayOfGPFieldInfoEx">
        <GPFieldInfoEx xsi:type="typens:GPFieldInfoEx">
            <Name>UNITCODE</Name>
            <AliasName>UNITCODE</AliasName>
            <ModelName>UNITCODE</ModelName>
            <FieldType>esriFieldTypeString</FieldType>
            <IsNullable>true</IsNullable>
            <Required>true</Required>
        </GPFieldInfoEx>
        <GPFieldInfoEx xsi:type="typens:GPFieldInfoEx">
            <Name>REGIONCODE</Name>
            <AliasName>REGIONCODE</AliasName>
            <ModelName>REGIONCODE</ModelName>
            <DomainName>DOM_REGIONCODE_NPS2016</DomainName>
            <FieldType>esriFieldTypeString</FieldType>
            <DefaultValueString>SER</DefaultValueString>
            <IsNullable>true</IsNullable>
            <Required>true</Required>
        </GPFieldInfoEx>
        <GPFieldInfoEx xsi:type="typens:GPFieldInfoEx">
            <Name>CREATEUSER</Name>
            <AliasName>CREATEUSER</AliasName>
            <ModelName>CREATEUSER</ModelName>
            <FieldType>esriFieldTypeString</FieldType>
            <DefaultValueString>GRSM User</DefaultValueString>
            <IsNullable>true</IsNullable>
            <Required>true</Required>
        </GPFieldInfoEx>
    </GPFieldInfoExs>
</DEFeatureClassInfo>');
-- DDL and sample data population, end

-- before
SELECT * FROM @tbl;

DECLARE @NewValue VARCHAR(10) = '';

UPDATE @tbl
SET XmlCol.modify('replace value of 
(/DEFeatureClassInfo/GPFieldInfoExs/GPFieldInfoEx[Name="CREATEUSER"]/Required/text())[1] with (sql:variable("@NewValue"))');

-- after
SELECT * FROM @tbl;