【问题标题】:regex expression for XML schemaXML 模式的正则表达式
【发布时间】:2020-04-19 20:06:58
【问题描述】:

我有这个 xml 内容,id 值从 1 到 193。

<member lang="en" id="UN193">
    <name>Zimbabwe</name>
    <admissionYear>1980</admissionYear>
    <population year="2019">14645468</population>
    <continent>Africa</continent>
    <officialLangInfo>
        <language script="Latin" family="Indo-European">English</language>
        <language script="Latin" family="Niger–Congo">Chewa</language>
        <language script="Latin" family="Niger–Congo">Kalanga</language>
        <otherLanguage>Chibarwe</otherLanguage>
    </officialLangInfo>
</member>

我创建了这个限制,这给了我一个错误验证文件。有什么建议?非常感谢。

    <xs:attribute name="id" use="required">
            <xs:simpleType>
                <xs:restriction base="xs:ID">
                    <xs:pattern value="[UN][0-9]{1,3}"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>

【问题讨论】:

  • 试试UN\d{1,3}
  • UN 不应放在方括号中。它们不是必需的。方括号表示 U 或 N,两者都需要。

标签: regex xml schema


【解决方案1】:

一种可能性是使用UN\[0-9]{1,3} 允许在UN0UN999 范围内的ID。

如果你想更精确,只允许 1 到 193 的 id,你可以试试UN(1[0-9][0-3]|[1-9][0-9]|[1-9])

例如here

【讨论】:

  • 除非您真的想允许非 ASCII 数字,否则最好使用 [0-9] 而不是 \d。此外,您可以使用1[0-9][0-9]|[1-9][0-9]|[1-9] 更精确一些
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-06
  • 1970-01-01
  • 1970-01-01
  • 2012-10-30
  • 1970-01-01
  • 1970-01-01
  • 2017-03-25
相关资源
最近更新 更多