sibprogrammer/xq

Does XQ support concat, and if so, how in the hell does it work?

DCameronMauch opened this issue · 10 comments

Problem Statement

Can't figure out how to use concat, if even supported. This is the best example I have found, and it produces no results: https://scrapfly.io/blog/how-to-join-values-in-xpath/

Expected Result

From the article above, I expect to see the output the articles says I should see.

cameron@CameronMacBookPro latest % cat example.xml
<html>
  <div>
    <h2>Coffee</h2>
    <span data-currency="USD">4.69</span>
  </div>
</html>
cameron@CameronMacBookPro latest % cat example.xml | xq -x '//div/concat(h2, " - ", span, span/@data-currency)'
cameron@CameronMacBookPro latest %

Unfortunately, it doesn't work as intended. I'll take a look at how to workaround the problem.

Thank you!

Now it's possible to use some popular functions. However, the support has some limitations. One of them is that the function will not work inside the selector but works well on the top level. It means you should use something like that:

$ cat example.xml | ./xq -x 'concat(//div/h2, " - ", //div/span, //div/span/@data-currency)'
Coffee - 4.69USD

Awesome, thanks! I'll give that a whirl.

When will this be generally available? I tried it just now, with version 1.2.3, and still getting no results.

cameron@CameronMacBookPro latest % cat example.xml
<html>
  <div>
    <h2>Coffee</h2>
    <span data-currency="USD">4.69</span>
  </div>
</html>
cameron@CameronMacBookPro latest % cat example.xml | xq -x 'concat(//div/h2, " - ", //div/span, //div/span/@data-currency)'
cameron@CameronMacBookPro latest % xq -v
xq version 1.2.3 (2023-10-19T18:59:37Z, 2842ec90a2d8143dd90211b083ace7325e6a3a8f)

The issue was addressed in the master branch, which points to 1.2.4 (see https://github.com/sibprogrammer/xq/blob/master/version) It is not released yet. But I think I'll release it today or tomorrow.

Thanks again!

FYI - Confirmed working

cameron@CameronMacBookPro latest % cat example.xml | xq -x 'concat(//div/h2, " - ", //div/span, //div/span/@data-currency)'
Coffee - 4.69USD

Is this also possible when using CSS selectors?