Based on this reply
So for example, we have XML like this
<parent_node >
<category>Low</category>
<category>Medium</category>
<category>High</category>
</parent_node>
and everything is good. But in my case i have a default XML namespace here, so my XML looks like this:
<parent_node xmlns="http://schemas.datacontract.org/2004/07/MonitorWang.Core.Interfaces.Entities">
<category>Low</category>
<category>Medium</category>
<category>High</category>
</parent_node>
and now it doesn't work.
Entire script:
DECLARE @XmlVariable XML = '<parent_node xmlns="http://schemas.datacontract.org/2004/07/MonitorWang.Core.Interfaces.Entities">
<category>Low</category>
<category>Medium</category>
<category>High</category>
</parent_node>'
SELECT
XTbl.Cats.value('.', 'varchar(50)')
FROM
@XmlVariable.nodes('/parent_node/category') AS XTbl(Cats)
Please, advice, how to fix it?
If you want to account for the default namespace throughout the document then you can use something like....
DECLARE @XmlVariable XML = '<parent_node xmlns="http://schemas.datacontract.org/2004/07/MonitorWang.Core.Interfaces.Entities">
<category>Low</category>
<category>Medium</category>
<category>High</category>
</parent_node>'
SELECT
XTbl.Cats.value('.', 'varchar(50)')
FROM
@XmlVariable.nodes('declare default element namespace "http://schemas.datacontract.org/2004/07/MonitorWang.Core.Interfaces.Entities"; /parent_node/category') AS XTbl(Cats)
Alternatively, you can declare the namespace in the xquery string and refer to it in the XPath if the default namespace is on a child element...
@XmlVariable.nodes('declare namespace c="http://schemas.datacontract.org/2004/07/MonitorWang.Core.Interfaces.Entities"; /parent_node/c:category')
There is more detail here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.