title: XML how to establish if a node exists in VBA before using an Xpath command on it
body: <p>I've some XML that <em>sometimes</em> has specific content that's present and other times it's not present in the XML. When this data is not present it causes my VBA parsing routine to barf. I therefore need to test a specific path to see if it exists prior to trying to grab the data.</p>
  
  <pre><code>Dim objxmldoc As New MSXML2.DOMDocument60   
  
  XMLHttpRequest.Open "GET", SignedURL, False
  
  XMLHttpRequest.send (SignedURL)
  
  objxmldoc.loadXML (XMLHttpRequest.responseXML.XML)
  
  xmlNamespaces = "xmlns:ns2='http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd' " _
                        &amp; "xmlns:ns1='http://mws.amazonservices.com/schema/Products/2011-10-01'"
  objxmldoc.SetProperty "SelectionNamespaces", xmlNamespaces
  
  
  Set NoOfASINs = objxmldoc.selectNodes("//ns1:GetCompetitivePricingForASINResult")
  
  i = 0
  For Each n In NoOfASINs
      Set objxmlASIN = objxmldoc.selectNodes("/ns1:ASIN")
      Set objXMLPrice = objxmldoc.selectNodes("/ns1:Product/ns1:CompetitivePricing/ns1:CompetitivePrices/ns1:CompetitivePrice") 
  
      Debug.Print objxmlASIN(i).text + "," + objXMLPrice(i).text 
      i = i + 1
  Next
  </code></pre>
  
  <p>Sometimes there's nothing underneath <code>/ns1:Product/ns1:CompetitivePricing/ns1:CompetitivePrices</code> ... so I need to test first.</p>
  
  <pre><code>&lt;GetCompetitivePricingForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01"&gt;
         &lt;GetCompetitivePricingForASINResult ASIN="B002NJHGN6" status="Success"&gt;
                &lt;Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd"&gt;
                       &lt;Identifiers&gt;
                              &lt;MarketplaceASIN&gt;
                                     &lt;MarketplaceId&gt;A1F83G8C2ARO7P&lt;/MarketplaceId&gt;
                                     &lt;ASIN&gt;B002NJHGN6&lt;/ASIN&gt;
                                 &lt;/MarketplaceASIN&gt;
                          &lt;/Identifiers&gt;
                       &lt;CompetitivePricing&gt;
                              &lt;CompetitivePrices&gt;
                                     &lt;CompetitivePrice belongsToRequester="true" condition="New" subcondition="New"&gt;
                                            &lt;CompetitivePriceId&gt;1&lt;/CompetitivePriceId&gt;
                                            &lt;Price&gt;
                                                   &lt;LandedPrice&gt;
                                                          &lt;CurrencyCode&gt;GBP&lt;/CurrencyCode&gt;
                                                          &lt;Amount&gt;17.69&lt;/Amount&gt;
                                                      &lt;/LandedPrice&gt;
                                                   &lt;ListingPrice&gt;
                                                          &lt;CurrencyCode&gt;GBP&lt;/CurrencyCode&gt;
                                                          &lt;Amount&gt;17.69&lt;/Amount&gt;
                                                      &lt;/ListingPrice&gt;
                                                   &lt;Shipping&gt;
                                                          &lt;CurrencyCode&gt;GBP&lt;/CurrencyCode&gt;
                                                          &lt;Amount&gt;0.00&lt;/Amount&gt;
                                                      &lt;/Shipping&gt;
                                               &lt;/Price&gt;
                                        &lt;/CompetitivePrice&gt;
                                 &lt;/CompetitivePrices&gt;
                              &lt;NumberOfOfferListings&gt;
                                     &lt;OfferListingCount condition="New"&gt;16&lt;/OfferListingCount&gt;
                                     &lt;OfferListingCount condition="Any"&gt;16&lt;/OfferListingCount&gt;
                                 &lt;/NumberOfOfferListings&gt;
                          &lt;/CompetitivePricing&gt;
                       &lt;SalesRankings&gt;
                              &lt;SalesRank&gt;
                                     &lt;ProductCategoryId&gt;home_improvement_display_on_website&lt;/ProductCategoryId&gt;
                                     &lt;Rank&gt;10299&lt;/Rank&gt;
                                 &lt;/SalesRank&gt;
                              &lt;SalesRank&gt;
                                     &lt;ProductCategoryId&gt;home_and_garden_display_on_website&lt;/ProductCategoryId&gt;
                                     &lt;Rank&gt;51823&lt;/Rank&gt;
                                 &lt;/SalesRank&gt;
                              &lt;SalesRank&gt;
                                     &lt;ProductCategoryId&gt;1939047031&lt;/ProductCategoryId&gt;
                                     &lt;Rank&gt;28&lt;/Rank&gt;
                                 &lt;/SalesRank&gt;
                          &lt;/SalesRankings&gt;
                   &lt;/Product&gt;
            &lt;/GetCompetitivePricingForASINResult&gt;
         &lt;GetCompetitivePricingForASINResult ASIN="B003F9MNF4" status="Success"&gt;
                &lt;Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd"&gt;
                       &lt;Identifiers&gt;
                              &lt;MarketplaceASIN&gt;
                                     &lt;MarketplaceId&gt;A1F83G8C2ARO7P&lt;/MarketplaceId&gt;
                                     &lt;ASIN&gt;B003F9MNF4&lt;/ASIN&gt;
                                 &lt;/MarketplaceASIN&gt;
                          &lt;/Identifiers&gt;
                       &lt;CompetitivePricing&gt;
                              &lt;CompetitivePrices&gt;&lt;/CompetitivePrices&gt;
                              &lt;NumberOfOfferListings&gt;
                                     &lt;OfferListingCount condition="New"&gt;2&lt;/OfferListingCount&gt;
                                     &lt;OfferListingCount condition="Any"&gt;2&lt;/OfferListingCount&gt;
                                 &lt;/NumberOfOfferListings&gt;
                          &lt;/CompetitivePricing&gt;
                       &lt;SalesRankings&gt;
                              &lt;SalesRank&gt;
                                     &lt;ProductCategoryId&gt;home_improvement_display_on_website&lt;/ProductCategoryId&gt;
                                     &lt;Rank&gt;141917&lt;/Rank&gt;
                                 &lt;/SalesRank&gt;
                              &lt;SalesRank&gt;
                                     &lt;ProductCategoryId&gt;home_and_garden_display_on_website&lt;/ProductCategoryId&gt;
                                     &lt;Rank&gt;892073&lt;/Rank&gt;
                                 &lt;/SalesRank&gt;
                              &lt;SalesRank&gt;
                                     &lt;ProductCategoryId&gt;1939038031&lt;/ProductCategoryId&gt;
                                     &lt;Rank&gt;868&lt;/Rank&gt;
                                 &lt;/SalesRank&gt;
                          &lt;/SalesRankings&gt;
                   &lt;/Product&gt;
            &lt;/GetCompetitivePricingForASINResult&gt;
         &lt;GetCompetitivePricingForASINResult ASIN="B002F9nNFY" status="Success"&gt;
                &lt;Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd"&gt;
                       &lt;Identifiers&gt;
                              &lt;MarketplaceASIN&gt;
                                     &lt;MarketplaceId&gt;A1F83G8C2ARO7P&lt;/MarketplaceId&gt;
                                     &lt;ASIN&gt;B002F9nNFY&lt;/ASIN&gt;
                                 &lt;/MarketplaceASIN&gt;
                          &lt;/Identifiers&gt;
                       &lt;CompetitivePricing&gt;
                              &lt;CompetitivePrices&gt;
                                     &lt;CompetitivePrice belongsToRequester="true" condition="New" subcondition="New"&gt;
                                            &lt;CompetitivePriceId&gt;1&lt;/CompetitivePriceId&gt;
                                            &lt;Price&gt;
                                                   &lt;LandedPrice&gt;
                                                          &lt;CurrencyCode&gt;GBP&lt;/CurrencyCode&gt;
                                                          &lt;Amount&gt;113.99&lt;/Amount&gt;
                                                      &lt;/LandedPrice&gt;
                                                   &lt;ListingPrice&gt;
                                                          &lt;CurrencyCode&gt;GBP&lt;/CurrencyCode&gt;
                                                          &lt;Amount&gt;113.99&lt;/Amount&gt;
                                                      &lt;/ListingPrice&gt;
                                                   &lt;Shipping&gt;
                                                          &lt;CurrencyCode&gt;GBP&lt;/CurrencyCode&gt;
                                                          &lt;Amount&gt;0.00&lt;/Amount&gt;
                                                      &lt;/Shipping&gt;
                                               &lt;/Price&gt;
                                        &lt;/CompetitivePrice&gt;
                                 &lt;/CompetitivePrices&gt;
                              &lt;NumberOfOfferListings&gt;
                                     &lt;OfferListingCount condition="New"&gt;2&lt;/OfferListingCount&gt;
                                     &lt;OfferListingCount condition="Any"&gt;2&lt;/OfferListingCount&gt;
                                 &lt;/NumberOfOfferListings&gt;
                          &lt;/CompetitivePricing&gt;
                       &lt;SalesRankings&gt;
                              &lt;SalesRank&gt;
                                     &lt;ProductCategoryId&gt;home_improvement_display_on_website&lt;/ProductCategoryId&gt;
                                     &lt;Rank&gt;170655&lt;/Rank&gt;
                                 &lt;/SalesRank&gt;
                              &lt;SalesRank&gt;
                                     &lt;ProductCategoryId&gt;home_and_garden_display_on_website&lt;/ProductCategoryId&gt;
                                     &lt;Rank&gt;1098520&lt;/Rank&gt;
                                 &lt;/SalesRank&gt;
                              &lt;SalesRank&gt;
                                     &lt;ProductCategoryId&gt;1939038031&lt;/ProductCategoryId&gt;
                                     &lt;Rank&gt;1031&lt;/Rank&gt;
                                 &lt;/SalesRank&gt;
                          &lt;/SalesRankings&gt;
                   &lt;/Product&gt;
            &lt;/GetCompetitivePricingForASINResult&gt;
         &lt;ResponseMetadata&gt;
                &lt;RequestId&gt;efa320b7-0162-49fc-a328-2f8d93f55f2f&lt;/RequestId&gt;
            &lt;/ResponseMetadata&gt;
     &lt;/GetCompetitivePricingForASINResponse&gt;
  </code></pre>