Editing xml using SQL
I'm new the xml data type in SQL Server. I have a field with some xml that I want to edit. In essence I want to delete certain nodes if they have a certain datatype or value. There are many subtype nodes and each subtype can have many SubtypeFieldInfo.
In cases where a SubtypeFieldInfo node contains a node :
<DefaultValue xsi:type="xs:dateTime">2222-02-22T00:00:00</DefaultValue>
I would like to delete that node
Here is the full xml. Any help much appreciated
<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.0" xsi:type="typens:DEFeatureClassInfo">
<Subtypes xsi:type="typens:ArrayOfSubtype">
<Subtype xsi:type="typens:Subtype">
<SubtypeName>1</SubtypeName>
<SubtypeCode>1</SubtypeCode>
<FieldInfos xsi:type="typens:ArrayOfSubtypeFieldInfo">
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>Status</FieldName>
<DomainName>pwStatus</DomainName>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>OwnedBy</FieldName>
<DomainName>shdMaintainedBy</DomainName>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>MaintainedBy</FieldName>
<DomainName>shdMaintainedBy</DomainName>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>InputCode</FieldName>
<DomainName />
<DefaultValue xsi:type="xs:int">12</DefaultValue>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>UnitType</FieldName>
<DomainName>trafStripingType</DomainName>
<DefaultValue xsi:type="xs:string">SY</DefaultValue>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>CalTDescription</FieldName>
<DomainName>trafStripingDescr</DomainName>
<DefaultValue xsi:type="xs:string">CL</DefaultValue>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>DetailNumber</FieldName>
<DomainName />
<DefaultValue xsi:type="xs:string">1</DefaultValue>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>StripingDetail</FieldName>
<DomainName>trafStripingDetail</DomainName>
<DefaultValue xsi:type="xs:string">2WYCENTERLINE</DefaultValue>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>Color</FieldName>
<DomainName>trafStripingColor</DomainName>
<DefaultValue xsi:type="xs:string">Yellow</DefaultValue>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>LinePattern</FieldName>
<DomainName>trafStripeLineType</DomainName>
<DefaultValue xsi:type="xs:string">Skip</DefaultValue>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>Material</FieldName>
<DomainName>trafStripingMaterial</DomainName>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>TravelDirection</FieldName>
<DomainName>trafTravelDirection</DomainName>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>IsSchoolZone</FieldName>
<DomainName>shdBooleanYesNo</DomainName>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>EstInstallDate</FieldName>
<DomainName />
<DefaultValue xsi:type="xs:dateTime">2222-02-22T00:00:00</DefaultValue>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>RefreshDate</FieldName>
<DomainName />
<DefaultValue xsi:type="xs:dateTime">2222-02-22T00:00:00</DefaultValue>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>WOUpdateDate</FieldName>
<DomainName />
<DefaultValue xsi:type="xs:dateTime">2222-02-22T00:00:00</DefaultValue>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>Quadrant</FieldName>
<DomainName>shdQuadrantZone</DomainName>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>DwgAsbuiltDate</FieldName>
<DomainName />
<DefaultValue xsi:type="xs:dateTime">2222-02-22T00:00:00</DefaultValue>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>DwgSignDate</FieldName>
<DomainName />
<DefaultValue xsi:type="xs:dateTime">2222-02-22T00:00:00</DefaultValue>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>AcceptanceDate</FieldName>
<DomainName开发者_运维知识库 />
<DefaultValue xsi:type="xs:dateTime">2222-02-22T00:00:00</DefaultValue>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>IsSyncHansen</FieldName>
<DomainName>shdBooleanYesNo</DomainName>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>ImageDate</FieldName>
<DomainName />
<DefaultValue xsi:type="xs:dateTime">2222-02-22T00:00:00</DefaultValue>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>ModifiedDate</FieldName>
<DomainName />
<DefaultValue xsi:type="xs:dateTime">2222-02-22T00:00:00</DefaultValue>
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>CreateDate</FieldName>
<DomainName />
<DefaultValue xsi:nil="true" />
</SubtypeFieldInfo>
<SubtypeFieldInfo xsi:type="typens:SubtypeFieldInfo">
<FieldName>DwgSheet</FieldName>
<DomainName />
<DefaultValue xsi:nil="true" />
</SubtypeFieldInfo>
</FieldInfos>
</Subtype>
Not sure what node you want to delete so here are two options.
Delete the DefaultValue
node:
update YourTable
set XMLCol.modify('delete (//SubtypeFieldInfo/DefaultValue)
[@xsi:type="xs:dateTime" and
text() = "2222-02-22T00:00:00"]')
Delete the SubtypeFieldInfo
node:
update YourTable
set XMLCol.modify('delete (//SubtypeFieldInfo)
[DefaultValue/@xsi:type="xs:dateTime" and
DefaultValue/text() = "2222-02-22T00:00:00"]')
精彩评论