0

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

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

thnx bdw what are the numbers in [] signify?
[1] says to take exactly first text value among all text values of nodes value1. In your document it is 'abc'. [2] says to take exactly second text value among all text values of nodes value1. In your document it is 'efg'.
Where do you update it? You just select. For example, you have an xml document as you mentioned above. What other xml document would you like to get after update?
it would be great if you could take a look here stackoverflow.com/questions/22643699/…
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.