XPath: if child exists - get value, if not - write empty line

#1

I have three node types: without categories, with a single category and multiple categories:

<technology>
  <categories>
    <category></category>
  </categories>
  <name></name>
</technologie>    

<technology>
  <name></name>
</technologie>    

<technology>
  <categories>
    <category></category>
    <category></category>
    <category></category>
  </categories>
  <name></name>
</technologie>

I want to write data from technology node like:

  • if there is a single category - write it,
  • if there is no category - write an empty line
  • if there are multiple categories - write the first one.

I tried //technology/categories/category[1] - it works, but partly. It fails on writing empty line in case category doesn't exist and writes all existing categories one after one.

Give me please a hint, how to write data in the outlined order.

The formula i use is:
=Dump(XPathOnUrl("https://api.builtwith.com/v12/api.xml?KEY=XXX&LOOKUP="&A2;"//technology/categories/category[1]";;;"text"))

PS: =IFERROR doesn't help too...

#2

I used a local test file for this, hence the file reference in the formula:

bild

=IF(VALUE(XPathOnUrl($A$1;"count(//xpath)"))>0;XPathOnUrl($A$1;"//xpath");"")

The count() function returns number of nodes, if it's 1 or more, it returns the first match, otherwise writes empty cell.