質問

say my xml doc is this

<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>

i have it a table newTable2 and xml datatyped column as xmlcol1

here is the query i worte

SELECT        xmlcol1.query('/root/parent/child/field/value1/text()') AS a
FROM            newTable2

this works when i remove the xmlns attribute if i put it back it does can anyone explain why is it so and how can i query for same keeping the xmlns attribute.

役に立ちましたか?

解決 2

never mind i found the answer i just need to use the ;WITH XMLNAMESPACES(DEFAULT 'http://www.w3.org/2001/XMLSchema-instance') before the query

他のヒント

Try this:

;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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top