说我的xml doc就是这个
<root xmlns="http://www.w3.org/2001/XMLSchema-instance">
<parent prop="1">
<child>
<field name="1">
<value1>abc</value1>
<value2>cdf</value2>
</field>
<field name="2">
<value1>efg</value1>
<value2>hjk</value2>
</field>
</child>
</parent>
<parent2>
<prop atrb="2">abc</prop>
</parent2>
</root>
我有一个表newTable2和xml数据类型列为xmlcol1
这是查询i worte
SELECT xmlcol1.query('/root/parent/child/field/value1/text()') AS a
FROM newTable2
当我删除xmlns属性时,如果我把它放回去,它可以解释为什么会这样,我怎样才能查询同样保留xmlns属性。
答案 0 :(得分:1)
试试这个:
;with xmlnamespaces (
default 'http://www.w3.org/2001/XMLSchema-instance'
)
SELECT xmlcol1.query('/root/parent/child/field/value1/text()') AS a_query
, xmlcol1.value('(/root/parent/child/field/value1/text())[1]', 'varchar(255)') AS a_value_1
, xmlcol1.value('(/root/parent/child/field/value1/text())[2]', 'varchar(255)') AS a_value_2
FROM newTable2
答案 1 :(得分:0)
;WITH XMLNAMESPACES(DEFAULT 'http://www.w3.org/2001/XMLSchema-instance')
在查询之前