benibela / xidel

Command line tool to download and extract data from HTML/XML pages or JSON-APIs, using CSS, XPath 3.0, XQuery 3.0, JSONiq or pattern matching. It can also create new or transformed XML/HTML/JSON documents.
http://www.videlibri.de/xidel.html
GNU General Public License v3.0
681 stars 42 forks source link

Problem with constructing proper command call #3

Closed noisy closed 8 years ago

noisy commented 8 years ago

On website http://videlibri.sourceforge.net/cgi-bin/xidelcgi

I found that query:

/document/offers/offer/concat(price/text(), for $r in . return 'default-value'[not($r/price/text())])

returns:

default-value
63400.00
69300.00
35000.00

for this xml

right now I am trying to construct arguments for xidel installed on my local machine, but I have problem with that.

Could you help me figure out how this call should look like? Is it Xpath 3 or 2?

benibela commented 8 years ago

Could you help me figure out how this call should look like?

xidel tesla.xml -e "/document/offers/offer/concat(price/text(), for $r in . return 'default-value'[not($r/price/text())])"

On linux you would need \$ or swap the quotes :

xidel tesla.xml -e '/document/offers/offer/concat(price/text(), for $r in . return "default-value"[not($r/price/text())])'

Is it Xpath 3 or 2?

It is XPath 2

In XPath 3 you could use let $r := return ...

Although it is a really weird approach. You can just use if:

xidel tesla.xml -e "/document/offers/offer/(if (price/text()) then price/text() else 'default-value')"

And drop the text. In the default settings it only shows text anyways:

xidel tesla.xml -e "/document/offers/offer/(if (price) then price else 'default-value')"

And then you can swap it back, but get the first:

xidel tesla.xml -e "/document/offers/offer/(price, 'default-value')[1]"