XSD 正则表达式:空字符串或其他内容
XSD regular expressions: empty string OR something else
我正在尝试通过 C# 或 js 强制执行我在 SEC 的 EDGAR 架构中找到的 XSD 正则表达式。
我有以下 XSD 简单类型:
<xs:simpleType name="ACCESSION_NUMBER_TYPE">
<xs:restriction base="xs:token">
<xs:pattern value="[*]{0}|[0-9]{1,10}\-[0-9]{1,2}\-[0-9]{1,6}"/>
</xs:restriction>
</xs:simpleType>
它恰好来自 eis_Common.xsd,包含在 zip 文件中,您可以 d/l 来自 SEC's EDGARLink Online page。在 eis_ABS_15GFiler.xsd 中可以找到几乎重复的定义,但该类型限制的基础是 xs:string
.
<xs:simpleType name="ACCESSION_NUMBER_TYPE">
<xs:restriction base="xs:string">
<xs:pattern value="[*]{0}|[0-9]{1,10}\-[0-9]{1,2}\-[0-9]{1,6}"/>
</xs:restriction>
</xs:simpleType>
对于上述模式,我认为可以允许空白或空值。我将上面的模式翻译为两个子句,或在一起。第一个子句 ([*]{0}
) 匹配...
the character class whose sole member is asterisk – C. M. Sperberg-McQueen
...零次,这意味着空字符串或空 XML 节点值。第二个子句匹配 ([0-9]{1,10}\-[0-9]{1,2}\-[0-9]{1,6}
) "one to ten digits, hyphen, one to two digits, hyphen, one to six digits".
但是 SEC 拒绝了与上述简单类型对应的具有 null 或空值的 XML 节点。
这一特殊模式在我的方法中是个例外。对于我测试过的所有其他简单类型,这些简单类型在 SEC 的 EDGAR 模式中通过正则表达式模式定义,包括多个模式和简单正则表达式类型的联合,我的方法有效。我要为其生成 XML 的这个表达式是有效的,但 SEC 拒绝了。
所以这是一个完整性检查。如果我包装上面的模式表达式 ^(<expr>)$
,并针对 null 或空字符串进行测试,它 在 C# 和 js 中匹配 , 由于第一个子句。正确的?我是否缺少有关 XSD 正则表达式的内容?
对于 js 示例,使用 regex101.com
风味:javascript
正则表达式:^([*]{0}|[0-9]{1,10}-[0-9]{1,2}-[0-9]{1,6}) $
修饰符:gm
测试字符串:
1-1-1
3
5
6-6-6
匹配:第 1、2、4、6 行
但 SEC 基本上告诉我表达式应该只匹配 1 和 6。
@kjhughes
No, a blank (single whitespace character) would not be allowed.
为了减轻混乱,我清理了一些冗长的文字并将 "blank" 替换为 "empty"。我的意思是一个在 C# (== ""
) 或 js (=== ""
) 中为空的字符串。我希望将其视为空值,并与 ^([*]{0}|...)$
(js: /^([*]{0}|...)$/
) 匹配。正在测试的 XML 片段最终将是:
...
<ns:ACCESSION_NUMBER_TYPE></ns:ACCESSION_NUMBER_TYPE>
...
Regular expressions in XSD are implicitly anchored at start and end with ^ and $.
我相信我理解 xsd 规范中关于隐式锚定的部分,这就是为什么我一直试图通过显式包装 xsd 模式将其转换为 C# 或 js 正则表达式验证在上面的示例中,在开始行中捕获结束行 (^(...)$
) 锚点。对于 js,它还会被包裹在 /.../
中。
这不是一个安全的假设吗?这适用于 EDGAR 模式中的所有其他模式,这些模式已被许多最终用户在多个月的过程中使用,并适用于几种不同的上下文。这是大约 60 种模式,我没有发现任何问题。
这就是为什么我有信心评估模式在 XSD 正则表达式范围内的实际含义,并且 我同意 以及您关于 null 值处理的回答 。你会把它扩展到一个 C#/js 空字符串,这会导致一个 XML 节点,就像我上面说明的那样吗?也许我已经超出了我自己的问题范围:D
For the above simple type, I would think that blank or null value
would be allowed.
是,允许空值(零长度字符串)。
不,空格(单个空白字符)不允许。
If I wrap the above pattern expression, ^()$, and test against a
null or blank string, it matches in both C# and js, due to the first
clause. Correct? Am I missing something about XSD regex?
XSD 中的正则表达式在开头和结尾隐式锚定为 ^
和 $
。
根据 spec:
Note: Unlike some popular regular expression languages (including
those defined by Perl and standard Unix utilities), the regular
expression language defined here implicitly anchors all regular
expressions at the head and tail, as the most common use of regular
expressions in ·pattern· is to match entire literals.
根据进一步的 OP 问题编辑进行更新
是的,非常具体,这个 XML:
<a></a>
对这个有效 XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="ACCESSION_NUMBER_TYPE">
<xs:restriction base="xs:string">
<xs:pattern value="[*]{0}|[0-9]{1,10}\-[0-9]{1,2}\-[0-9]{1,6}"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="a" type="ACCESSION_NUMBER_TYPE"/>
</xs:schema>
Would you extend it to a C#/js empty string, which would result in an
XML node like I have illustrated above?
如上所示 a
等空元素的字符串值在 C#、JavaScript、Java、Python 或任何其他语言。
我正在尝试通过 C# 或 js 强制执行我在 SEC 的 EDGAR 架构中找到的 XSD 正则表达式。
我有以下 XSD 简单类型:
<xs:simpleType name="ACCESSION_NUMBER_TYPE">
<xs:restriction base="xs:token">
<xs:pattern value="[*]{0}|[0-9]{1,10}\-[0-9]{1,2}\-[0-9]{1,6}"/>
</xs:restriction>
</xs:simpleType>
它恰好来自 eis_Common.xsd,包含在 zip 文件中,您可以 d/l 来自 SEC's EDGARLink Online page。在 eis_ABS_15GFiler.xsd 中可以找到几乎重复的定义,但该类型限制的基础是 xs:string
.
<xs:simpleType name="ACCESSION_NUMBER_TYPE">
<xs:restriction base="xs:string">
<xs:pattern value="[*]{0}|[0-9]{1,10}\-[0-9]{1,2}\-[0-9]{1,6}"/>
</xs:restriction>
</xs:simpleType>
对于上述模式,我认为可以允许空白或空值。我将上面的模式翻译为两个子句,或在一起。第一个子句 ([*]{0}
) 匹配...
the character class whose sole member is asterisk – C. M. Sperberg-McQueen
...零次,这意味着空字符串或空 XML 节点值。第二个子句匹配 ([0-9]{1,10}\-[0-9]{1,2}\-[0-9]{1,6}
) "one to ten digits, hyphen, one to two digits, hyphen, one to six digits".
但是 SEC 拒绝了与上述简单类型对应的具有 null 或空值的 XML 节点。
这一特殊模式在我的方法中是个例外。对于我测试过的所有其他简单类型,这些简单类型在 SEC 的 EDGAR 模式中通过正则表达式模式定义,包括多个模式和简单正则表达式类型的联合,我的方法有效。我要为其生成 XML 的这个表达式是有效的,但 SEC 拒绝了。
所以这是一个完整性检查。如果我包装上面的模式表达式 ^(<expr>)$
,并针对 null 或空字符串进行测试,它 在 C# 和 js 中匹配 , 由于第一个子句。正确的?我是否缺少有关 XSD 正则表达式的内容?
对于 js 示例,使用 regex101.com
风味:javascript
正则表达式:^([*]{0}|[0-9]{1,10}-[0-9]{1,2}-[0-9]{1,6}) $
修饰符:gm
测试字符串:
1-1-1
3
5
6-6-6
匹配:第 1、2、4、6 行
但 SEC 基本上告诉我表达式应该只匹配 1 和 6。
@kjhughes
No, a blank (single whitespace character) would not be allowed.
为了减轻混乱,我清理了一些冗长的文字并将 "blank" 替换为 "empty"。我的意思是一个在 C# (== ""
) 或 js (=== ""
) 中为空的字符串。我希望将其视为空值,并与 ^([*]{0}|...)$
(js: /^([*]{0}|...)$/
) 匹配。正在测试的 XML 片段最终将是:
...
<ns:ACCESSION_NUMBER_TYPE></ns:ACCESSION_NUMBER_TYPE>
...
Regular expressions in XSD are implicitly anchored at start and end with ^ and $.
我相信我理解 xsd 规范中关于隐式锚定的部分,这就是为什么我一直试图通过显式包装 xsd 模式将其转换为 C# 或 js 正则表达式验证在上面的示例中,在开始行中捕获结束行 (^(...)$
) 锚点。对于 js,它还会被包裹在 /.../
中。
这不是一个安全的假设吗?这适用于 EDGAR 模式中的所有其他模式,这些模式已被许多最终用户在多个月的过程中使用,并适用于几种不同的上下文。这是大约 60 种模式,我没有发现任何问题。
这就是为什么我有信心评估模式在 XSD 正则表达式范围内的实际含义,并且 我同意 以及您关于 null 值处理的回答 。你会把它扩展到一个 C#/js 空字符串,这会导致一个 XML 节点,就像我上面说明的那样吗?也许我已经超出了我自己的问题范围:D
For the above simple type, I would think that blank or null value would be allowed.
是,允许空值(零长度字符串)。
不,空格(单个空白字符)不允许。
If I wrap the above pattern expression, ^()$, and test against a null or blank string, it matches in both C# and js, due to the first clause. Correct? Am I missing something about XSD regex?
XSD 中的正则表达式在开头和结尾隐式锚定为 ^
和 $
。
根据 spec:
Note: Unlike some popular regular expression languages (including those defined by Perl and standard Unix utilities), the regular expression language defined here implicitly anchors all regular expressions at the head and tail, as the most common use of regular expressions in ·pattern· is to match entire literals.
根据进一步的 OP 问题编辑进行更新
是的,非常具体,这个 XML:
<a></a>
对这个有效 XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="ACCESSION_NUMBER_TYPE">
<xs:restriction base="xs:string">
<xs:pattern value="[*]{0}|[0-9]{1,10}\-[0-9]{1,2}\-[0-9]{1,6}"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="a" type="ACCESSION_NUMBER_TYPE"/>
</xs:schema>
Would you extend it to a C#/js empty string, which would result in an XML node like I have illustrated above?
如上所示 a
等空元素的字符串值在 C#、JavaScript、Java、Python 或任何其他语言。