Search code examples
xmlgoogle-sheets

What is the correct syntax for using an XPath predicate in Google Sheets ImportXML?


I have the following XML results from an ImportXML function in my sheet:

<lfm status="ok">
<toptags artist="Madonna">
  <tag>
    <count>100</count>
    <name>pop</name>
    <url>https://www.last.fm/tag/pop</url>
  </tag>
  <tag>
    <count>50</count>
    <name>dance</name>
    <url>https://www.last.fm/tag/dance</url>
  </tag>
  <tag>
    <count>50</count>
    <name>female vocalists</name>
    <url>https://www.last.fm/tag/female+vocalists</url>
  </tag>
  <tag>
    <count>35</count>
    <name>80s</name>
    <url>https://www.last.fm/tag/80s</url>
  </tag>
  <tag>
    <count>28</count>
    <name>electronic</name>
    <url>https://www.last.fm/tag/electronic</url>
  </tag>
  <tag>
    <count>9</count>
    <name>seen live</name>
    <url>https://www.last.fm/tag/seen+live</url>
  </tag>
</toptags>
</lfm>

I'm interested in filtering out the "seen live" tag from this list, and just getting the names of the rest. I'd expect my results to look like "pop", "dance", "female vocalists", electronic".

I've tried to wrap the query with and without quotes, like "//tag[@name != 'seen live']" and "//tag['@name != seen live']", but nothing seems to work. I've tried double quotes as well. I can only get results when the whole inner square bracket portion is enclosed in quotes, but no filtering every seems to work.

Is there a different syntax necessary than what's mentioned here? Or is another function like .contains() required?


Solution

  • Your XPath,

    //tag[@name != 'seen live']
    

    is testing whether an attribute name is not equal to that string.

    Your XML has an element name, not an attribute, so change the XPath to

    //tag[name != 'seen live']