根据条件删除SQL Server中的XML节点

时间:2013-01-11 12:35:04

标签: sql-server xml

我的xml看起来像

<MeasureSet>
    <MeasureSetType val_type="name">Variant</MeasureSetType>
    <Measure>
      <MeasureType val_type="name">single nucleotide variant</MeasureType>
      <AttributeSet>
        <MeasureAttributeType val_type="name">HGVS</MeasureAttributeType>
        <Attribute>NM_000054.4:c.838dupT</Attribute>
      </AttributeSet>
      <Citation>
        <CitationType val_type="name">general</CitationType>
        <ID Source="PubMed">10820168</ID>
      </Citation>
      <Citation>
        <CitationType val_type="name">general</CitationType>
        <ID Source="PubMed" />
      </Citation>
      <Citation>
        <CitationType val_type="name">general</CitationType>
        <ID Source="PubMed">9773787</ID>
      </Citation>
      <Citation>
        <CitationType val_type="name">general</CitationType>
        <ID Source="PubMed">18726898</ID>
      </Citation>
      <Citation>
        <CitationType val_type="name">general</CitationType>
        <ID Source="PubMed" />
      </Citation>
    </Measure>
  </MeasureSet>

我想删除Citation / ID / @ Source =“PubMed”且Citation / ID为空的Citation元素节点

这只会删除ID元素,但会删除正确的元素

SET @myBlob.modify('delete //Citation/ID[@Source = ''PubMed''
      and string-length(string(number(.))) = 0]') 

并删除所有引用元素

SET @myBlob.modify('delete //Citation[ID/@Source = ''PubMed''
      and string-length(string(number(.))) = 0]') 

似乎应该有一个简单的解决方案我不打算。有什么建议?感谢

1 个答案:

答案 0 :(得分:2)

你的第二个是检查错误元素的长度 - 它正在查看(.),这里是Citation元素(因为它位于Citation元素的谓词中)。改为使用

SET @myBlob.modify('
      delete //Citation[ID/@Source = ''PubMed'' 
                        and string-length(string(number(ID[1]))) = 0]') 

我已将.更改为ID[1]。需要[1],因为“'number()' requires a singleton (or empty sequence)”。