Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>SELECT SubmittedDate, XML.query('data(/Transaction01/POHeader/PO_NBR)') as PO_NBR FROM SubmitXML </code></pre> <p>You original <code>XPath</code>, <code>/POHeader/PO_NBR</code>, assumed that <code>POHeader</code> is the root node (which is not).</p> <p>A sample query to check:</p> <pre><code>DECLARE @myxml XML SET @myxml = ' &lt;Transaction01&gt; &lt;TransactionSetPurpose&gt;Insert&lt;/TransactionSetPurpose&gt; &lt;POHeader&gt; &lt;PO_NBR&gt;LG40016181&lt;/PO_NBR&gt; &lt;/POHeader&gt; &lt;/Transaction01&gt;' SELECT @myxml.query('data(/Transaction01/POHeader/PO_NBR)') </code></pre> <p>If <code>Transaction01</code> is not always the root node (which is not a good thing), use this:</p> <pre><code>SELECT SubmittedDate, XML.query('data(/*/POHeader/PO_NBR)') as PO_NBR FROM SubmitXML </code></pre> <p>Generally, <code>XML</code> schema 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:</p> <pre><code>&lt;Transaction id='01'&gt; &lt;TransactionSetPurpose&gt;Insert&lt;/TransactionSetPurpose&gt; &lt;POHeader&gt; &lt;PO_NBR&gt;LG40016181&lt;/PO_NBR&gt; &lt;/POHeader&gt; &lt;/Transaction&gt; </code></pre> <p><strong>Update:</strong></p> <p>You should declare the namespaces using <code>WITH XMLNAMESPACES</code>:</p> <pre><code>DECLARE @myxml XML SET @myxml = ' &lt;Transaction01 xmlns="http://services.iesltd.com/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt; &lt;TransactionSetPurpose&gt;Insert&lt;/TransactionSetPurpose&gt; &lt;POHeader&gt; &lt;PO_NBR&gt;LG40016181&lt;/PO_NBR&gt; &lt;/POHeader&gt; &lt;/Transaction01&gt;' ; WITH XMLNAMESPACES ( 'http://services.iesltd.com/' AS m ) SELECT @myxml.query ( 'data(/*/m:POHeader/m:PO_NBR)' ) </code></pre> <p><strong>Update 2:</strong></p> <p>To sort:</p> <pre><code>; WITH XMLNAMESPACES ( 'http://services.iesltd.com/' AS m ) SELECT SubmittedDate, XML.value('(/*/m:POHeader/m:PO_NBR)[1]', 'NVARCHAR(200)') AS po_nbr FROM SubmitXML ORDER BY po_nbr </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload