我有一个问题。我在 XML 中有换行符、空格和制表符。像这样:
<value xs:type="DV_TEXT"><value>1111\this is what it is used for, this could be a
really long line or even
multiple lines, just like
what you are reading now
</value></value>
来自 org.w3c.dom 的 Java 中的 setTextContent 和 getTextContent 处理得很好。没问题。
但是现在,我正在生成 Schematron 进行验证,以检查该字符串是否确实出现在值中。 Schematron 是从配置测试字符串的定义文件生成的
生成的 Schematron,断言测试如下所示:
test="(matches(.,'1111\this is what it is used for, this could be a really long line or even
multiple lines, just like
what you are reading now'))"
然后当我验证时,会出现更多问题。
首先是换行符。似乎在生成 Schematron 的定义文件中有 \r\n
而不是只有 \n
。
但好吧,我必须指望这一点。如果我仅将所有
替换为
,一些错误就会消失。我如何确定 XML 文件也只有
作为换行符?
我认为我需要更改测试断言中的字符串,例如,仅将所有 \r\n
替换为 \n
。
我已经这样做了,它部分解决了我的问题。我还应该考虑什么?
欢迎提供所有建议。
最佳答案
如果您希望节点文本无论其空白如何都有效,请使用 normalize-space function 功能:
The normalize-space function returns the argument string with whitespace normalized by stripping leading and trailing whitespace and replacing sequences of whitespace characters by a single space. [...]
所以,这应该有效:
test="(matches(normalize-space(.),'1111\this is what it is used for, this could be a really long line or even multiple lines, just like what you are reading now'))
关于java - XML Schematron 中的换行符和空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27801569/