Using MS SQL Server, I’ve got some data in an XML field (called XML), which is structured like this:
<Transaction01>
<TransactionSetPurpose>Insert</TransactionSetPurpose>
<POHeader>
<PO_NBR>LG40016181</PO_NBR>
</POHeader>
</Transaction01>
I’m trying to create a SQL query to fetch another column called SubmittedDate, along with the PO_NBR from this XML field. Being new to XPath, I’ve read numerous examples and tried both query and value, but I’ve not been successful yet. For example:
SELECT SubmittedDate,
XML.query('data(/POHeader/PO_NBR)') as PO_NBR
FROM SubmitXML
This just gives me a empty column. After getting a working test from Quassnoi, I worked from his XML to mine, and discovered the problem is the xmlns and xmlns:i attributes in the root node:
<Transaction01 xmlns="http://services.iesltd.com/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
So how do I get around that?
You original
XPath,/POHeader/PO_NBR, assumed thatPOHeaderis the root node (which is not).A sample query to check:
If
Transaction01is not always the root node (which is not a good thing), use this:Generally,
XMLschema assumes that the tag names are fixed and the variable parts go to the data of the nodes and the attributes rather than into their names, like this:Update:
You should declare the namespaces using
WITH XMLNAMESPACES:Update 2:
To sort: