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

时间:2022-08-03 20:24:33

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

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

<PostedDate>42183296</PostedDate>

and space-filling is also allowed:

和空间填充也是允许的:

<PostedDate>        </PostedDate>

which led me to the XSD:

这让我进入了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:

与XSD中的情况一样,大多数格式都无法使用XSD轻松表示,因此我选择尝试使用正则表达式规则:

.{8} | ""

which trying to convert to XSD i type:

尝试转换为XSD我输入:

<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?

奖励:任何人都可以在XSD文档中指出\ d匹配数字的位置吗?或者其他特殊模式代码是什么?

2 个解决方案

#1


11  

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"

我猜可能会看起来像\ d {8} |这意味着“八位数或无数字”,但不是八位数或两位数。然而,这并没有解释,为什么20101111不匹配。您确定元素值中没有空格或其他附加符号吗?据说\ d匹配“F.1.1 Character Class Escapes”部分中的数字

#2


3  

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

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

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

#1


11  

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"

我猜可能会看起来像\ d {8} |这意味着“八位数或无数字”,但不是八位数或两位数。然而,这并没有解释,为什么20101111不匹配。您确定元素值中没有空格或其他附加符号吗?据说\ d匹配“F.1.1 Character Class Escapes”部分中的数字

#2


3  

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

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

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