2

序章

SQL Server 2008 で xml 列を照会しようとしていますが、修正できないエラーが発生します。

これは私が使用するスキーマです:

CREATE XML SCHEMA COLLECTION PublicationSchema AS '
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">
   <xs:import namespace="http://www.w3.org/XML/1998/namespace"
              schemaLocation="xml.xsd"/>
   <xs:element name="publication">
      <xs:complexType>
         <xs:sequence>
            <xs:element ref="metadata"/>
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   <xs:element name="meta">
      <xs:complexType>
         <xs:attributeGroup ref="attlist-meta"/>
      </xs:complexType>
   </xs:element>
   <xs:attributeGroup name="attlist-meta">
      <xs:attribute name="name" use="required"/>
      <xs:attribute name="content"/>
      <xs:attribute name="scheme"/>
   </xs:attributeGroup>
   <xs:element name="metadata">
      <xs:complexType>
         <xs:sequence>
            <xs:element maxOccurs="unbounded" ref="meta"/>
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   </xs:schema>'
GO

スキーマを使用して XML 列を持つテーブルを作成します: create table test (content XML(PublicationSchema))

いくつかのデータを挿入します:

insert into test values(N'<?xml version="1.0" encoding="UTF-16"?>
<publication>
    <metadata>
        <meta name="type" content="plan" scheme="city"/>
        <meta name="statistics" content="second" scheme="informationtype"/>
    </metadata>
</publication>')

問題

クエリを実行すると:

select * from test
where Content.exist('/publication/metadata/meta[@name] = "type"') = 1

次のエラーが表示されます。

 Msg 2213, Level 16, State 1, Line 3
 XQuery [test.content.exist()]: Cannot atomize/apply data()
    on expression that contains type 'meta' within inferred
    type 'element(meta,#anonymous) *'

質問

このクエリを修正するために何ができるか知っている人はいますか?

4

1 に答える 1

2

関数に構文エラーがありますexist。ブラケット間の比較が必要です。

select * 
from test
where Content.exist('/publication/metadata/meta[@name = "type"]') = 1

これは、スキーマに対応していない場合でも、所有している XML で問題なく機能します。そのスキーマを適用すると、属性のデータ型がないため、コメントで言及したエラーが発生しますname
これを修正するには、2 つのオプションがあります。スキーマを変更してデータ型を含めるか、上記のクエリを書き直して、SQL Server をだまして属性をスキーマの一部として扱わないようにします。

のデータ型を指定すると、次のnameようになります。

<xs:attributeGroup name="attlist-meta">
   <xs:attribute name="name" use="required" type="xs:string"/>
   <xs:attribute name="content"/>
   <xs:attribute name="scheme"/>
</xs:attributeGroup>

スキーマを変更できない場合は、代わりにこのクエリを使用できます。

select *
from test
where Content.query('/publication/metadata/meta').exist('/*[@name = "type"]') = 1
于 2012-06-02T13:52:34.130 に答える