在XPath中紧凑地插入两个连续的换行:

huangapple go评论46阅读模式
英文:

Compact way to insert two newlines together in XPath

问题

在XPath中,我想在使用concat(…)和/或string-join(…)时插入一些换行符。从https://stackoverflow.com/q/36312794中,我了解到我可以使用codepoints-to-string(10)插入一个换行符。对于两个换行符,我可以使用concat(codepoints-to-string(10), codepoints-to-string(10)),但这似乎有点冗长。从https://stackoverflow.com/q/43136756中,我了解到我可以明显地使用string-join((1 to 2)!codepoints-to-string(10))或类似的东西,但我不确定这是否会有很大的改进。

在XPath中表示两个或更多连续的换行符是否有更紧凑的方法?

英文:

In XPath I want to insert some newlines when using concat(…) and/or string-join(…). From https://stackoverflow.com/q/36312794 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 https://stackoverflow.com/q/43136756 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?

答案1

得分: 2

code-points-to-string(…) 可以提供多个代码点,但必须将它们提供为序列:codepoints-to-string((10, 10))

然而,正如Michael Kay所指出的,更直接的方法是在'…'内部插入你想要的换行符,无需转义(我曾以为这是不可能的,因为在许多查询语言中插入换行符会破坏查询)。

如何插入文字换行符取决于宿主语言。例如,在Java中,你需要在Java Stringchar级别提供一些转义,例如 "'…\n\n'"(或者还有许多其他可能性,比如 StringBuilder.append((char)10))。

如果你在XML中指定XPath查询,你应该直接在XPath表达式中添加 


关键是传递到XPath处理器的字符串没有任何转义,它包含了文字U+00A0代码点。

英文:

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.

huangapple
  • 本文由 发表于 2023年6月19日 21:45:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定