XPATH inserting linefeed

Hi,

I’m working on a website scraper using XPATH. First a wrote a little javascript to “kill” some incompatible HTML so a valid (kind of) XML remains. So I chained the result to a XPATH expression to get the desired results. It works, so now I’m trying to format to result to a nice string to display on my dashboard.

Problem is: I can’t find a way to insert linefeeds into the result. Assuming it’s not openHAB specific googled, but it seems all solutions don’t work in openHAB. Or I’m searching wrong. Anyway a good moment to ask for help.

Below some of the state transformations I tried. Technically they work, but the result is just the “lifefeed” solution-text instead of a line feed.

JS:html.js∩XPATH:concat( (//div[@class='row'])[1], '
', (//div[@class='row'])[2] )
JS:html.js∩XPATH:concat( (//div[@class='row'])[1], '\n', (//div[@class='row'])[2] )
JS:html.js∩XPATH:concat( (//div[@class='row'])[1], 'codepoints-to-string(10)', (//div[@class='row'])[2] )
JS:html.js∩XPATH:concat( (//div[@class='row'])[1], '
', (//div[@class='row'])[2] )
JS:html.js∩XPATH:concat( (//div[@class='row'])[1], '<br>', (//div[@class='row'])[2] )

update, tested a bit using https://www.freeformatter.com/xpath-tester.html with this simple XML and XPATH

<xml><boo>10</boo></xml>
concat(//boo, codepoints-to-string(10),//boo)

Above is the only working solution of all the things I’ve found. However, removing the quotes around codepoint-to-string(10) in openHAB results in an exception of the XPATH transformation (no details supplied in log). Doesn’t it support this function? Or does it need a different syntax?

As far as I know, neither sitemaps nor MainUI support newlines where representing states of Items. I’m not certain about HABPanel. It might support rendering a <br>.

1 Like

Interesting, never realized that. But yeah, hardcoding a ‘\n’ in an item value also only displays the code instead of actually showing a linebreak. Same for ‘<br>’ in the MainUI (that I use). The XPATH should still support the codepoints-to-string() function instead of just passing the function name as text. But knowing openHAB won’t render a linebreak anyway makes this less relevant.

But you got me on a solution! I’ve change the XPATH to separate using a pipe symbol. And passing this to an oh-repeater makes it possible to show the result as separate lines.

(XPATH)
JS:html.js∩XPATH:concat( (//div[@class='row'])[1], '|', (//div[@class='row'])[2] )

(partial code from widget)
slots:
  default:
    - component: oh-repeater
      config:
        for: bus
        fragment: true
        in: =items[props.item].state.split("|")
        sourceType: array
      slots:
        default:
          - component: oh-list-item
            config:
              iconN: f7:arrowtriangle_right_circle_fill
              title: = loop.bus

One more question. So now the XPATH result is hardcoded to 2 items. I can change that to 4 (the max amount of results on the website). But XPATH has a function string-join for that, but it seems openHAB doesn’t support that one?

JS:html.js∩XPATH:string-join(//div[@class='row'],  '|')

I’m not super familiar with teh XPath transformation but it is true that for most transformations that OH supports, there are limitations and minor differences from standards to account for how OH works (e.g. JSONPATH cannot work well with variable length arrays). This might be one of those cases.

Thanx! It works, code perfection is no goal. My display shows the schedule of departing busses at my stop again. Mission accomplished. I’ll mark the OH-repeater as a solution for others.