I’m trying to get value of cat but it retuns me null. Please, help me to get this value. Thanks in advance!
DECLARE @xml XML
SET @xml = '
<cat:catalog xmlns:cat="http://datypic.com/cat"
xmlns:prod="http://datypic.com/prod">
<cat:number>1446</cat:number>
<prod:product>
<prod:number>563</prod:number>
<prod:name prod:language="en">Floppy Sun Hat</prod:name>
</prod:product>
</cat:catalog>'
Select @xml.value('declare namespace ns="xmlns:cat=http://datypic.com/cat"; /ns:cat[1]/number[1]', 'int')
You are trying to grab “cat[1]” but there is no element “cat”, it is only a prefix. You want “catalog” I believe. Also, you must namespace prefix the “number” child as well. And the way you wrote declare namespace is I believe incorrect as well. No need for “xmlns:cat=” in there.
In other words:
…ought to work. But podiluska’s prefix change is more suitable since “ns” doesn’t give any hint about what namespace you are using. The important thing to bear in mind is that the prefix is arbitrary (it could be anything), but you have to tie it to whatever element you want in that namespace.