本文介绍了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):

我不明白为什么.在 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.

推荐答案

您需要将 ^ $ 删除为 XSD模式隐式锚定,并且 ^ $ 不用作锚定这些模式,并修复 [a-zA-z] 字符类中的拼写错误,如果需要匹配任何ASCII,则应为 [a-zA-Z] 字母(请参见此相关答案[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模式模式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 23:32