title: Parse XML using SQL Server 2016
body: <p>I'm trying to clean up some SQL code. Currently I have an XML like:</p>
  
  <pre class="lang-sql prettyprint-override"><code>DECLARE @xml xml = N'&lt;PubmedArticleSet&gt;
    &lt;PubmedArticle&gt;
      &lt;MedlineCitation Status="MEDLINE" Owner="NLM"&gt;           
        &lt;ArticleIdList&gt;           
          &lt;ArticleId IdType="pii"&gt;0092-8674(92)90516-F&lt;/ArticleId&gt;
          &lt;ArticleId IdType="pubmed"&gt;1423608&lt;/ArticleId&gt;
          &lt;ArticleId IdType="doi"&gt;10.1016/0092-8674(92)90516-f&lt;/ArticleId&gt;
        &lt;/ArticleIdList&gt;
      &lt;/PubmedData&gt;
    &lt;/PubmedArticle&gt;
  &lt;/PubmedArticleSet&gt;'
  </code></pre>
  
  <p>And I need to get the value of the <code>ArticleId</code> with the attribute of </p>
  
  <pre class="lang-xml prettyprint-override"><code>    IdType ="pubmed"
  </code></pre>
  
  <p>I would like something like and <code>XQUERY</code> method that could:</p>
  
  <pre class="lang-sql prettyprint-override"><code>DECLARE @PMID NVARCHAR(15)
  SET @PMID =  @xml.value('(//PubmedData/ArticleIdList/ArticleId/@IdType)[1]', 'varchar(15)' ) 
  </code></pre>
  
  <p>But that pulls the IdType "pii" of the first node. And</p>
  
  <pre class="lang-sql prettyprint-override"><code>SET @PMID =  @xml.value('(//PubmedData/ArticleIdList/ArticleId/@IdType = "pubmed")[1]', 'varchar(15)' ) 
  </code></pre>
  
  <p>Brings back true.</p>
  
  <p>Is there a way to filter based upon "pubmed" and then get the number 1423608?  So <code>@PMID</code> would then be equal to 1423608.</p>