我需要指定某些XML元素始终不为null且不为空。我找到this question,建议使用限制将minLength
设置为1.但是有几张海报建议使用带有正则表达式的模式限制来代替minLength
或者除minLength
之外限制。正则表达式优于<root> ... </root>
来实现此目的的优势是什么?
答案 0 :(得分:0)
使用正则表达式可以在指定有效性时区分空白字符和非空白字符,而不是单独依赖长度。
例如,没有正则表达式,#!/bin/bash
# note that this requires bash 4.0 or later
mapfile -t lines < <(npm ls --dev --parseable) # read content into array
lines=( "${lines[@]##*/}" ) # trim everything prior to last / in each
(IFS='|'; printf '%s\n' "${lines[*]}") # emit array as a single string with |s
,
NonEmptyString
允许<xs:simpleType name="NonEmptyString">
<xs:restriction base="xs:string">
<xs:minLength value="1" />
</xs:restriction>
</xs:simpleType>
(但不是<x> </x>
或<x/>
)。 这可能是您想要的也可能不是。
然而,使用正则表达式,<x></x>
,
NonEmptyStringWithoutSpaces
不允许<xs:simpleType name="NonEmptyStringWithoutSpaces">
<xs:restriction base="xs:string">
<xs:pattern value="\S+"/> <!-- one or more non-whitespace chars -->
</xs:restriction>
</xs:simpleType>
(并且仍然不允许<x> </x>
或<x/>
。但请注意,这也不允许<x></x>
。 这可能不是您想要的。
如果您想允许嵌入空格,可以使用
<x>A B</x>
到不允许<xs:simpleType name="NonEmptyNonBlankString">
<xs:restriction base="xs:string">
<xs:pattern value=".*\S.*"/> <!-- at least one non-whitespace char -->
</xs:restriction>
</xs:simpleType>
,<x> </x>
或<x/>
,同时允许<x></x>
和<x>A B</x>
。或者,如果没有正则表达式,您可以使用xs:whiteSpace
方面:
<x> A </x>
这可能是你想要的。