Tsql sqlxquery;类型化XML中的更新属性

Tsql sqlxquery;类型化XML中的更新属性,tsql,xquery-sql,Tsql,Xquery Sql,我正在尝试更新类型化XML中的属性。在过去,我通过在非类型化对象中构建XML,然后将其设置为类型化对象来实现这一点(这样可以避免这个问题),但我想知道如何直接修改类型化数据 我的模式是: if exists (select [xml_collection_id] from sys.[xml_schema_collections] as [xsc] where [xsc].name = 'xsc_test_stack'

我正在尝试更新类型化XML中的属性。在过去,我通过在非类型化对象中构建XML,然后将其设置为类型化对象来实现这一点(这样可以避免这个问题),但我想知道如何直接修改类型化数据

我的模式是:

if exists (select [xml_collection_id]
           from   sys.[xml_schema_collections] as [xsc]
           where  [xsc].name = 'xsc_test_stack'
                  and [xsc].[schema_id] = schema_id(N'chamomile'))
  drop xml schema collection [chamomile].[xsc_test_stack];

go

create xml schema collection [chamomile].[xsc_test_stack] as N'<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:chamomile="https://github.com/KELightsey/chamomile" targetNamespace="https://github.com/KELightsey/chamomile">
  <xsd:element name="test_stack">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="description" type="xsd:string" minOccurs="1" maxOccurs="1" />
        <xsd:element name="test_stack_detail" type="chamomile:any_complex_type" minOccurs="0" maxOccurs="unbounded" />
        <xsd:element name="test" type="chamomile:any_complex_type" minOccurs="0" maxOccurs="unbounded" />
      </xsd:sequence>
      <xsd:attribute name="name" type="xsd:string" use="required" />
      <xsd:attribute name="test_count" type="xsd:int" use="required" />
      <xsd:attribute name="pass_count" type="xsd:int" use="required" />
      <xsd:attribute name="timestamp" type="xsd:dateTime" use="required" />
     <xsd:anyAttribute processContents="lax" />
    </xsd:complexType>
  </xsd:element>

    <xsd:complexType name="any_complex_type">
        <xsd:complexContent>
            <xsd:restriction base="xsd:anyType">
                <xsd:sequence>
                    <xsd:any minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
                </xsd:sequence>
                <xsd:anyAttribute processContents="lax" />
            </xsd:restriction>
        </xsd:complexContent>
    </xsd:complexType>
</xsd:schema>';

go 
declare @test [xml]([chamomile].[xsc_test_stack]) = N'
     <chamomile:test_stack xmlns:chamomile="https://github.com/KELightsey/chamomile" name="[chamomile].[person_test].[get_age]" test_count="2" pass_count="2" timestamp="2018-06-24T15:50:19.3466667">
       <description>This test stack consists of tests which validate the functionality of the age calculation for a person.</description>
     </chamomile:test_stack>';
go

declare @test [xml]([chamomile].[xsc_test_stack]) = N'
     <chamomile:test_stack xmlns:chamomile="https://github.com/KELightsey/chamomile" name="[chamomile].[person_test].[get_age]" test_count="2" pass_count="2" timestamp="2018-06-24T15:50:19.3466667">
       <description>This test stack consists of tests which validate the functionality of the age calculation for a person.</description>
       <test_stack_detail>
          <any_valid_xml_goes_here />
       </test_stack_detail>
     </chamomile:test_stack>';
go
set @test_stack.modify(N'replace value of (//@test_count)[1] with sql:variable("@count")');
返回:Msg 9306,16级,状态1,第28行 XQuery[modify()]:找不到“replace value of”的目标是联合类型(attribute(test_count,xs:int)| attribute(test_count,xs:anySimpleType))

返回:Msg 9306,16级,状态1,第25行 XQuery[modify()]:找不到“replace value of”的目标是联合类型(attribute(test_count,xs:int)| attribute(test_count,xs:anySimpleType))

我花了几个小时在谷歌上搜索这个。有很多非类型化XML的示例,还有一些类型化XML的示例仍然抛出相同的异常

我希望能有一些见解

谢谢,
凯瑟琳

我必须承认,我也没有找到任何直接的方法。似乎是个错误,至少我看不出这有什么意义。变通方法是一种简单的强制转换,但这与您在最初几行中描述的变通方法非常接近:

declare@test[xml]([洋甘菊][xsc_test_stack])=
不
此测试堆栈由验证人员年龄计算功能的测试组成。
';
声明@cnt INT=99;
声明@intermediate XML=CAST(@test为XML)--魔法在这里发生
SET@intermediate.modify('声明名称空间洋甘菊='https://github.com/KELightsey/chamomile"; 
将(洋甘菊:test_stack/@test_count)[1]的值替换为sql:variable(“@cnt”);
设置@test=@中间值--重新分配给类型化XML
选择@test;

我接受这个答案,因为我认为你是对的;这是一个错误。我将继续使用非类型生成器,然后将其设置为类型化输出。谢谢你看它Shnugo!我不能让这件事过去。我已经查到了那个。我从开始使用,它确实替换了类型化xml中属性的值,并不断删减,直到我发现这是奇怪错误的来源。我将在几天后发布修改后的代码和示例。
set @test_stack.modify(N'declare namespace chamomile="https://github.com/KELightsey/chamomile"; 
    replace value of (chamomile:test_stack/@test_count)[1] with sql:variable("@count")');