1

I'm trying to optionally set $_url to "not-found" if there's no href.

  xidel --trace-stack --ignore-namespaces 'https://www.uline.com/Grp_41/Peanuts-and-Dispensers' --user-agent 'Mozilla/5.0 Firefox/94.0.1' \
    -f '//(a[@class="cssa"]|a[@class="subgroupName"])/@href' \
    --xml --xquery '
        let $title := //(h1|div[@class="item-result-desc"]/a)/text()

        return <product>
  <title>{$title}</title>
  <url>{$url}</url>
  {
    for $model in //(table[@class="subgroupChart"]|table[@class="item-result-price-grid__table"])//tr[position()>2 and not(contains(@class, "subgroupChartHeader"))]
      let $link := $model//td[1]//(a|span)
      let $name := $link/text()
      let $_url := $link/(string(@href), "not-found")
      let $price := $model//(td[contains(@class, "PriceCellwebPrice")][1]//attrib|td[contains(@class, "qtyBreaksPriceColumn")][1]//span)/text()
        return <model>
      { if ($name) then
        <name>{$name}</name>
        else()
      }
      { if ($price) then
          <price>{$price}</price>
        else ()
      }
      { if ($_url) then
        <url>https://uline.com{$_url}</url>
        else () 
      }
  </model>
  }
</product>'

Sometimes its a link sometimes its a span.

chovy
  • 72,281
  • 52
  • 227
  • 295

1 Answers1

1

I think you want let $_url := $link/(@href/string(), "not-found")[1] as (@href/string(), "not-found") forms the string sequence of the value of any existing href attribute plus the string literal "not-found" and with the [1] it will return the attribute value if one exists or the constant if the attribute doesn't exist.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110