1

再会,

以下测试有效:

    DECLARE @XmlTest TABLE
    ( ID int,
      MyContent Xml
    )

    declare @aaa XML

    set @aaa = '<Root><MyNode Qualifier="0" Code="AAA" Deleted="true">9.99</MyNode><MyNode Qualifier="0" Code="AAA"         Deleted="false">10.99</MyNode></Root>'
    insert into @XmlTest (ID, MyContent)
    values(1,@aaa)
    set @aaa = '<Root><MyNode Qualifier="0" Code="BBB" Deleted="false">9.99</MyNode></Root>'
    insert into @XmlTest (ID, MyContent)
    values(2,@aaa)

    select * from @XmlTest

-- 节点选择 [@Code="AAA" and @Deleted="false"] 是唯一的,所以我希望能够在逻辑上“删除它”

    UPDATE @XmlTest
    SET MyContent.modify('replace value of (/Root/MyNode[@Code="AAA" and @Deleted="false"]/@Deleted)[1] with true()')
    WHERE ID = 1

    select * from @XmlTest

但是对于 XSD,它不起作用。我有一条错误消息,例如

消息 2234、Niveau 16、État 1、Ligne 19 XQuery [@XmlTest.MyContent.modify()]:运算符“=”不能应用于“”和“xs:string”

这是我的表的架构和新声明

    DECLARE @XmlTest TABLE
    ( ID int,
      MyContent Xml(CONTENT TestSchema)
    )


    DROP XML SCHEMA COLLECTION TestSchema

    create xml schema collection TestSchema as
    '<?xml version="1.0" encoding="UTF-8" ?>
    <xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
    <xs:simpleType name="AmountType">
        <xs:restriction base="xs:decimal">
            <xs:minInclusive value="0"/>
            <xs:maxInclusive value="9999999999"/>
            <xs:fractionDigits value="2"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:element name="Root" sql:is-constant="1">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="MyNode" maxOccurs="unbounded" minOccurs="1" sql:is-constant="1">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="AmountType">
                                <xs:attribute name="Qualifier" use="required">
                                    <xs:simpleType>
                                       <xs:restriction base="xs:integer">
                                           <xs:minInclusive value="0"/>
                                       </xs:restriction>
                                    </xs:simpleType>
                                </xs:attribute>
                                <xs:attribute name="Code" use="required">
                                    <xs:simpleType>
                                        <xs:restriction base="xs:string">
                                            <xs:maxLength value="15"/>
                                        </xs:restriction>
                                    </xs:simpleType>
                                </xs:attribute>
                                <xs:attribute name="Deleted" use="required">
                                    <xs:simpleType>
                                        <xs:restriction base="xs:boolean">
                                           <xs:pattern value="true"/>
                                           <xs:pattern value="false"/>
                                        </xs:restriction>
                                    </xs:simpleType>
                                </xs:attribute>
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    </xs:schema>'

我尝试了这种修改,但如果以同样的方式失败:

    <xs:attribute type="xs:boolean" name="Deleted" use="required" />

提前致谢

4

1 回答 1

1

将已删除的定义更改为

<xs:attribute type="xs:boolean" name="Deleted" use="required" />

我不知道你为什么要以另一种方式来做。

查询仍然不起作用的原因是因为您将布尔值@Deleted与字符串进行比较"false"。改为使用false()

replace value of (/Root/MyNode[@Code="AAA" and @Deleted=false()]/@Deleted)[1] 
with true()
于 2013-03-09T22:05:54.407 回答