title: Is there a single XPath expression that can get a value from two different locations?
body: <p>I am using javax XPath to extract a value from some xml that can be returned from a service in one of two formats (don't ask me why), as shown below:</p>
  
  <pre><code>&lt;item&gt;
      &lt;attrQualOptMany name="someName"&gt;
          &lt;value&gt;someValueICareAbout&lt;/value&gt;
          &lt;value qual="101"&gt;someValueICareAbout&lt;/value&gt;
      &lt;/attrQualOptMany&gt;
  &lt;/item&gt;
  </code></pre>
  
  <p>or</p>
  
  <pre><code>&lt;item&gt;
      &lt;attr name="someName"&gt;someValueICareAbout&lt;/attr&gt;
  &lt;/item&gt;
  </code></pre>
  
  <p>Is there a <b>single</b> XPath expression that can pluck</p>
  
  <pre><code>someValueICareAbout 
  </code></pre>
  
  <p>from the</p>
  
  <pre><code>&lt;item&gt;&lt;/item&gt;
  </code></pre>
  
  <p>element in either case?  Since I don't know which xml format will be returned ahead of time, I am always having to resolve the value by evaluating two XPath expressions against the item element, as shown below.  There are actually several attributes of the item that can exist in either form, and I would prefer not to have scores of if-else blocks in my code to extract them if at all possible:</p>
  
  <pre><code>XPath xpath = XPathFactory.newInstance().newXPath();
  Element itemElement;  //This element has already been populated by prior code
  String value = xpath.compile("attrQualOptMany[@name='someName']/value/text()")
                      .evaluate(itemElement, XPathConstants.STRING);
  if(value  == null || value .isEmpty()) {
      value = xpath.compile("attr[@name='someName']/text()")
                   .evaluate(itemElement, XPathConstants.STRING);
  }
  </code></pre>