0

In XPath I want to insert some newlines when using concat(…) and/or string-join(…). From Line breaks (\n) via concat() in XPath? I learned I can insert a newline using codepoints-to-string(10). For two of them I can do concat(codepoints-to-string(10), codepoints-to-string(10)), but that seems sort of long. From concatenate a string/character n number of times in xpath I learned I can apparently do string-join((1 to 2)!codepoints-to-string(10)) or something close, but I'm not sure that that brings much improvement.

Is there some more compact way to represent two or more newlines together in XPath?

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
Garret Wilson
  • 18,219
  • 30
  • 144
  • 272

1 Answers1

2

It turns out that you can provide multiple code points to code-points-to-string(…), but you have to provide them as a sequence: codepoints-to-string((10, 10)).

However a more direct approach, as Michael Kay pointed out, is to simply insert as many newlines you want into the XPath expression inside '…', with no escaping needed. (I had assumed this wasn't possible, as inserting newlines into many query languages will wreck the query.)

How you insert literal newlines depends on the host language. For example in Java you need to provide some escaping at the Java String or char level, e.g. "'…\n\n'" (or one of many, many other possibilities, such as StringBuilder.append((char)10).

If you are specifying the XPath query in XML, you would add 
 right in the XPath expression.

The point is that the string that reaches the XPath processor has no escaping—it contains literal U+00A0 code points.

Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
  • 2
    Readers should note that codepoints-to-string() requires XPath 2.0. For XPath 4.0 a new function is proposed, `char('\n')`. Also note you can always use the escaping conventions of the host language, e.g. ` ` in XML, and `\n` in Java etc. – Michael Kay Jun 19 '23 at 17:14
  • I didn't realize until your answer in https://stackoverflow.com/a/76526853 what you meant by "escaping conventions of the host language". In other words the XPath string accepts literal U+00A0 newlines as actual code points, with no escaping required (other than whatever the host language requires). I'll update the answer when my brain is rested from today's XPath wrangling. – Garret Wilson Jun 21 '23 at 21:14