XML模式模式错误 [英] XML Schema Pattern Error

查看:51
本文介绍了XML模式模式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写XML模式.具体要求我验证一个字段,使其以字母开头,后跟字母数字字符(例如Foo3x4有效,3Foo或Foo3_无效).

I am trying to write an XML schema. The specifics require me to validate a field so that it starts with a letter, followed by alphanumeric characters (e.g. Foo3x4 is valid, 3Foo or Foo3_ are not).

这是我的写法:

<xsd:simpleType name="nameType">
    <xsd:restriction base="xsd:string">
        <xsd:pattern value="^[a-zA-z][a-zA-Z0-9]*$" />
    </xsd:restriction>
</xsd:simpleType>

但是,如果我尝试验证具有nameType值H0的文档,则会出现以下错误(由我翻译成英文):

But if I try to validate a document that has as nameType the value H0, it gives me the following error (translated in English by me):

cvc-pattern-valid:值"H [0]"对于类型名称类型"类型为"^ [a-zA-z] [a-zA-Z0-9] * $"的构面无效>

cvc-pattern-valid: The value "H0" is not valid as facet for the pattern "^[a-zA-z][a-zA-Z0-9]*$" for the type nameType

我不明白为什么.在 RegExr (/^ [a-zA-z] [a-zA-Z0-9] * $/gm ),它可以正常工作.我在模式规范中缺少什么吗?

I can't get why. Using it on RegExr (/^[a-zA-z][a-zA-Z0-9]*$/gm), it works. Am I missing something in the pattern specification?

最后一个细节.验证错误由JAXB Java框架中的编组给出.

One last detail. The validation error is given by the marshaller in the JAXB Java framework.

推荐答案

您需要将 ^ $ 删除为此相关答案[Az] 不仅与ASCII字母匹配.

You need to remove ^ and $ as XSD patterns are anchored implicitly and ^ and $ are not used as anchors in these patterns, and fix the typo in the [a-zA-z] character class, it should be [a-zA-Z] if you need to match any ASCII letter (see this related answer, [A-z] does not only match ASCII letters).

使用

<xsd:pattern value="[a-zA-Z][a-zA-Z0-9]*" />

此模式实际上将与以下模式匹配

This pattern will actually match the following

  • 隐式匹配字符串的开头
  • [a-zA-Z] -任意ASCII字母
  • [a-zA-Z0-9] * -零个或多个ASCII字母和/或数字.
  • 隐式匹配字符串的结尾
  • implicitly matches the start of string
  • [a-zA-Z] - any ASCII letter
  • [a-zA-Z0-9]* - zero or more ASCII letters and/or digits.
  • implicitly matches the end of string

这篇关于XML模式模式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆