Search code examples
xmlxqueryxidel

How to deal with empty node in xquery?


I want to print a TSV output like this. But when $x/span does not find anything, there will be only two columns printed. How to insert an empty field in the 2nd column when $x/span finds nothing? Thanks.

for $x in //td/b/a[@title]/parent::b/parent::td
return join(
    (
        $x/b/a/@title
        , $x/span
        , $x/p
    )
    , x:cps(9)
)

Solution

  • XPath:

    //td[b/a[@title]]/join(
      (
        b/a/@title,
        (span,"")[1],
        p
      ),
      x:cps(9)
    )
    

    XQuery:

    for $x in //td[b/a[@title]] return
    $x/join(
      (
        b/a/@title,
        (span,"")[1],
        p
      ),
      "	"
    )