XSD 正则表达式模式:这个或什么都没有

发布于 2024-10-03 21:50:33 字数 1774 浏览 0 评论 0原文

我正在尝试在 XSD 中定义一个方案规则,其中字符串长度为 8 个字符:

<PostedDate>42183296</PostedDate>

并且还允许填充空格:

<PostedDate>        </PostedDate>

这导致我找到 XSD:

<xs:simpleType name="DateFormat">
   <xs:restriction base="xs:string">
      <xs:length value="8" />            //exactly 8 characters long
</xs:simpleType>

但值可以也为空(即零字符长):

<PostedDate></PostedDate>
<PostedDate />

这导致我天真地尝试:

<xs:simpleType name="DateFormat">
   <xs:restriction base="xs:string">
      <xs:length value="8" />            //exactly 8 characters long
      <xs:length value="0" />            //exactly 0 characters long
</xs:simpleType>

这当然是不允许的。

正如 XSD 中常见的情况,大多数格式无法轻松地用 XSD 表示,因此我选择尝试正则表达式规则:

.{8} | ""

尝试转换为 XSD 我输入:

<xs:simpleType name="DateFormat">
    <xs:restriction base="xs:string">
        <xs:pattern value=".{8}|''" />
    </xs:restriction>
</xs:simpleType>

但它不起作用:

''20101111' is not facet-valid with respect to pattern '.{8}|''' for type 'DateFormat'

我也尝试了

  • < ;xs:pattern value="[0-9]{8}|''" />

任何其他人都可以解决解决问题的模式吗问题匹配 - 一些特定的模式 - 空

奖励:任何人都可以指出XSD 文档中的位置< /a> 表示 \d 匹配数字?或者还有哪些特殊的图案代码?

i'm trying to define an scheme rule in XSD, for which a string is 8 characters long:

<PostedDate>42183296</PostedDate>

and space-filling is also allowed:

<PostedDate>        </PostedDate>

which led me to the XSD:

<xs:simpleType name="DateFormat">
   <xs:restriction base="xs:string">
      <xs:length value="8" />            //exactly 8 characters long
</xs:simpleType>

but the value can also be empty (i.e. zero characters long):

<PostedDate></PostedDate>
<PostedDate />

which led me to naively try:

<xs:simpleType name="DateFormat">
   <xs:restriction base="xs:string">
      <xs:length value="8" />            //exactly 8 characters long
      <xs:length value="0" />            //exactly 0 characters long
</xs:simpleType>

Which of course isn't allowed.

As is often the case in XSD, most formats cannot be represented easily with XSD, so i opted to try a regular expression rule:

.{8} | ""

which trying to convert to XSD i type:

<xs:simpleType name="DateFormat">
    <xs:restriction base="xs:string">
        <xs:pattern value=".{8}|''" />
    </xs:restriction>
</xs:simpleType>

But it didn't work:

''20101111' is not facet-valid with respect to pattern '.{8}|''' for type 'DateFormat'

i also tried

  • <xs:pattern value="[0-9]{8}|''" />
  • <xs:pattern value="([0-9]{8})|('')" />
  • <xs:pattern value="(\d{8})|('')" />

Can anyone else thing of a pattern that solves the issue matching either
- some specific pattern
- empty

Bonus: can anyone point to the place in the XSD documentation that says that \d matches digits? Or what the other special pattern codes are?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜还是长夜 2024-10-10 21:50:33

我可能猜测模式应该看起来像 \d{8}| ,意思是“八位数字或什么都没有”,但不是八位数字或两个引号。但这并不能解释为什么 20101111 不匹配。您确定元素值中没有空格或其他附加符号吗?
\d 据说匹配“F.1.1 字符类转义”部分中的数字

I may guess that patters should look like \d{8}| that means "eight digits or nothing", but not eight digits or two quotes. However this doesn't explain, why 20101111 is not matched. Are you sure that there are no whitespaces or other additional symbols in the element value?
\d is said to match digits in the section "F.1.1 Character Class Escapes"

疏忽 2024-10-10 21:50:33

我也在同样的情况下允许空字符串,否则它必须是 6 个长度的数字。最后我用了像下面这样的。这对我有用

<xs:simpleType name="DateFormat">
    <xs:restriction base="xs:string">
        <xs:pattern value="|[0-9]{8}" />
    </xs:restriction>
</xs:simpleType>

I also in the same situation like empty string is allowed otherwise it must 6 length numbers. At last I used like the following. This works for me

<xs:simpleType name="DateFormat">
    <xs:restriction base="xs:string">
        <xs:pattern value="|[0-9]{8}" />
    </xs:restriction>
</xs:simpleType>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文