Xpath – selecting element by attribute value

Using the sample data below, I wanted to get all the Link node that have LinkID = 13 (not the Junk node).

Sample data

<whatever>
  <Links>
      <Link LinkID='13' /> 
      <Junk LinkID='13' />
      <Link LinkID='1' /> 
      <Link LinkID='13' /> 
  </Links>
</whatever>

What fails are these:

//*[local-name()=’Link’]/*[@LinkID=’13’] (XPath returned 0 items)
//*[local-name()=’Links’]/[local-name()=’Links’ and @LinkID=’13’] ( Unexpected token “[” in path expression or Expression must evaluate to a node-set)
//[local-name()=’Links’]//*[local-name()=’Link’][@LinkID=’13’] ( Unexpected token “[” in path expression or Expression must evaluate to a node-set) – This is same as one that worked below except // instead of //*

What works are these:

//*[local-name()=’Link’ and @LinkID=’13’]
//*[local-name()=’Link’][@LinkID=’13’]
//*[local-name()=’Links’]//*[local-name()=’Link’][@LinkID=’13’]

Uncategorized  

Leave a Reply