1

I am working with XML data on an SQL Server. The (exemplary) SQL looks as follows:

<Document xmlns="urn:iso:std:iso:20022:some:test:xmlns">
  <Testnode>a</Testnode>
</Document>

The XML is available in a table with a column named <fata of type XML.

My question is: How can I create a SELECT query that shows the text of the namespace in one column?

The expected output should be:

+----------------------------------------+
| xmlns                                  |
+----------------------------------------+
| urn:iso:std:iso:20022:some:test:xmlns  |
+----------------------------------------+

The result column should be a character string (no XML).

So far I have tried this query, however the result is NULL:

SELECT Data.value('(./Document)[1]','nvarchar(max)') AS xmlns
FROM xmltable

1 Answer 1

2

You can try something along this:

DECLARE @xml XML=
N'<Document xmlns="urn:iso:std:iso:20022:some:test:xmlns">
  <Testnode>a</Testnode>
</Document>';

--The XQuery-function namespace-uri() takes a singleton and returns its namespace uri

SELECT @xml.value('namespace-uri((/*:Document)[1])','nvarchar(max)');

As the <Document> element is living within the default namespace itself, we would have to know the namespace in advance in order to declare it. But - luckily - we can use the wildcard with *:.

Another option - one of the rare cases - is the usage of the outdated FROM OPENXML:

Try this:

DECLARE @xml XML=
N'<Document xmlns="urn:iso:std:iso:20022:some:test:xmlns">
  <Testnode>a</Testnode>
</Document>';

DECLARE @docHandle INT;
EXEC sp_xml_preparedocument @docHandle OUTPUT, @xml;  
SELECT * FROM OPENXML (@docHandle, '/*',1);  
EXEC sp_xml_removedocument @docHandle; 

This returns the complete XML with a lot of meta-data:

+----+----------+----------+-----------+--------+---------------------------------------+----------+------+---------------------------------------+
| id | parentid | nodetype | localname | prefix | namespaceuri                          | datatype | prev | text                                  |
+----+----------+----------+-----------+--------+---------------------------------------+----------+------+---------------------------------------+
| 0  | NULL     | 1        | Document  | NULL   | urn:iso:std:iso:20022:some:test:xmlns | NULL     | NULL | NULL                                  |
+----+----------+----------+-----------+--------+---------------------------------------+----------+------+---------------------------------------+
| 2  | 0        | 2        | xmlns     | xmlns  | NULL                                  | NULL     | NULL | NULL                                  |
+----+----------+----------+-----------+--------+---------------------------------------+----------+------+---------------------------------------+
| 4  | 2        | 3        | #text     | NULL   | NULL                                  | NULL     | NULL | urn:iso:std:iso:20022:some:test:xmlns |
+----+----------+----------+-----------+--------+---------------------------------------+----------+------+---------------------------------------+
| 3  | 0        | 1        | Testnode  | NULL   | urn:iso:std:iso:20022:some:test:xmlns | NULL     | NULL | NULL                                  |
+----+----------+----------+-----------+--------+---------------------------------------+----------+------+---------------------------------------+
| 5  | 3        | 3        | #text     | NULL   | NULL                                  | NULL     | NULL | a                                     |
+----+----------+----------+-----------+--------+---------------------------------------+----------+------+---------------------------------------+
Sign up to request clarification or add additional context in comments.

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.