Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在类型化xml列上选择节点的原子值_Sql_Sql Server 2008_Xpath - Fatal编程技术网

Sql 在类型化xml列上选择节点的原子值

Sql 在类型化xml列上选择节点的原子值,sql,sql-server-2008,xpath,Sql,Sql Server 2008,Xpath,如果不能在类型化xml列上使用文本,如何获取该列中元素的原子值 此查询 -- Add schema CREATE XML SCHEMA COLLECTION MySchema AS N'<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault=

如果不能在类型化xml列上使用文本,如何获取该列中元素的原子值

此查询

-- Add schema
CREATE XML SCHEMA COLLECTION MySchema AS N'<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            attributeFormDefault="unqualified"
            elementFormDefault="qualified"
            xmlns="http://mynamespace"
            targetNamespace="http://mynamespace">

  <xs:element name="element1" type="element1Type">
  </xs:element>

  <xs:complexType name="element1Type">
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element name="element2" type="element2Type" />
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="element2Type">
    <xs:sequence>
      <xs:element name="element3" type="element3Type" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="element3Type">
    <xs:sequence minOccurs="0">
      <xs:element minOccurs="0" name="element4" />
    </xs:sequence>
  </xs:complexType>

</xs:schema>'
GO

-- Create table
CREATE TABLE [dbo].[MyTable](
    [XmlColumn] [xml](CONTENT [dbo].[MySchema]) NOT NULL
)
GO

-- Add test data
INSERT INTO MyTable SELECT N'<element1 xmlns="http://mynamespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <element2>
    <element3>
      <element4>my text here</element4>
    </element3>
  </element2>
</element1>'
GO

-- run query
WITH XMLNAMESPACES(
    'http://mynamespace' as s,
    DEFAULT 'http://mynamespace'
)
SELECT 
    T.rows.value('(element3/element4)[1]/text()', 'varchar(100)') as [AtomicValue]
FROM
    MyTable
CROSS APPLY
    XmlColumn.nodes('/element1/element2') T(rows)
导致

XQuery [MyTable.XmlColumn.value()]: 'text()' is not supported on simple typed or 'http://www.w3.org/2001/XMLSchema#anyType' elements, found 'element({http://mynamespace}:element4,xs:anyType) ?'. 我已经读过了,但是对我希望能够读取的每个原子值大规模编辑模式感觉太过分了…

您可以使用query,然后使用value

也可以在模式中指定element4的类型

<xs:element minOccurs="0" name="element4" type="xs:string"/>

如果您可以添加您使用的模式和显示错误的示例XML,这将非常有帮助。是的,查看结构会很好。您是否尝试使用项目或节点而不是文本?@Mikael添加了完整复制query@András using node返回相同的异常。我不确定项的语法。。。
<xs:element minOccurs="0" name="element4" type="xs:string"/>
WITH XMLNAMESPACES(
    'http://mynamespace' as s,
    DEFAULT 'http://mynamespace'
)
SELECT 
    T.rows.value('(element3/element4)[1]', 'varchar(100)') as [AtomicValue]
FROM
    MyTable
CROSS APPLY
    XmlColumn.nodes('/element1/element2') T(rows)