XML模式定义中的正则表达式失败

时间:2021-06-27 17:17:07

I get the upcoming error while validating the XML against the schema.

在针对模式验证XML时,我收到了即将发生的错误。

Value 'this/is/a/simple/node-path' is not facet-valid 
with respect to pattern '^(\w+[\w\-/])+\w' for type 'PathModel'.

The definition of type PathModel is defined as a simpleType as the following snippet shows. It is used by <path>this/is/a/simple/node-path</path>

PathModel类型的定义定义为simpleType,如下面的代码段所示。它由 使用/是/ a / simple / node-path

<xs:simpleType name="PathModel">
  <xs:restriction base="xs:string">
    <xs:pattern value="^(\w+[\w\-/])+\w" />
  </xs:restriction>
</xs:simpleType>

The expected result is listed in this matching table.

预期结果列在此匹配表中。

this/is/a/simple/node-path       MATCHING
/this/is/a/simple/node-path      NOT MATCHING
this/is/a/simple/node-path/      NOT MATCHING
this/is/a/simple/nodep%th        NOT MATCHING (special characters)

What is going wrong? Thank you

出了什么问题?谢谢

2 个解决方案

#1


5  

Remove the leading ^ character.

删除前导^字符。


<xs:simpleType name="PathModel">
  <xs:restriction base="xs:string">
    <xs:pattern value="(\w+[\w\-/])+\w" />
  </xs:restriction>
</xs:simpleType>

This is the only valid value from the set you provided:

这是您提供的集合中唯一有效的值:


this/is/a/simple/node-path

This should do the trick for you (tested in my Eclipse IDE).

这应该是你的技巧(在我的Eclipse IDE中测试)。

The reason is visible, for example, here: http://www.regular-expressions.info/xml.html

原因很明显,例如:http://www.regular-expressions.info/xml.html

"Compared with other regular expression flavors, the XML schema flavor is quite limited in features. Since it's only used to validate whether an entire element matches a pattern or not, rather than for extracting matches from large blocks of data. XML schema always implicitly anchors the entire regular expression. The regex must match the whole element for the element to be considered valid. If you have the pattern regexp, the XML schema validator will apply it in the same way as say Perl, Java or .NET would do with the pattern ^regexp$."

“与其他正则表达式风格相比,XML模式风格在功能上非常有限。因为它仅用于验证整个元素是否与模式匹配,而不是用于从大块数据中提取匹配.XML模式总是隐含锚定正则表达式。正则表达式必须匹配元素的整个元素才能被认为是有效的。如果你有模式regexp,XML模式验证器将以与Perl,Java或.NET相同的方式应用它。 pattern ^ regexp $。“

#2


-2  

As far as I can see the regular expression seems to be valid and should give the results shown in your matching table, according to the XSD specification. Using Microsoft .NET Framework 2 with a test case using your path values and your regular expression I get exactly your expected results.

据我所知,正则表达式似乎是有效的,并且应该根据XSD规范给出匹配表中显示的结果。使用Microsoft .NET Framework 2和测试用例使用您的路径值和正则表达式,我得到了您期望的结果。

So, possible reasons for what you are seeing:

所以,你所看到的可能原因:

  • There is a bug in the XSD implementation you are using.
  • 您正在使用的XSD实施中存在一个错误。
  • Your code isn't validating what you think it is validating (though this seems unlikely given the error message you are seeing)
  • 您的代码未验证您认为它正在验证的内容(尽管根据您看到的错误消息,这似乎不太可能)

If you tell us what implementation you are using and post your code, it may be possible to help further.

如果您告诉我们您正在使用的实施方式并发布您的代码,则可以进一步提供帮助。

#1


5  

Remove the leading ^ character.

删除前导^字符。


<xs:simpleType name="PathModel">
  <xs:restriction base="xs:string">
    <xs:pattern value="(\w+[\w\-/])+\w" />
  </xs:restriction>
</xs:simpleType>

This is the only valid value from the set you provided:

这是您提供的集合中唯一有效的值:


this/is/a/simple/node-path

This should do the trick for you (tested in my Eclipse IDE).

这应该是你的技巧(在我的Eclipse IDE中测试)。

The reason is visible, for example, here: http://www.regular-expressions.info/xml.html

原因很明显,例如:http://www.regular-expressions.info/xml.html

"Compared with other regular expression flavors, the XML schema flavor is quite limited in features. Since it's only used to validate whether an entire element matches a pattern or not, rather than for extracting matches from large blocks of data. XML schema always implicitly anchors the entire regular expression. The regex must match the whole element for the element to be considered valid. If you have the pattern regexp, the XML schema validator will apply it in the same way as say Perl, Java or .NET would do with the pattern ^regexp$."

“与其他正则表达式风格相比,XML模式风格在功能上非常有限。因为它仅用于验证整个元素是否与模式匹配,而不是用于从大块数据中提取匹配.XML模式总是隐含锚定正则表达式。正则表达式必须匹配元素的整个元素才能被认为是有效的。如果你有模式regexp,XML模式验证器将以与Perl,Java或.NET相同的方式应用它。 pattern ^ regexp $。“

#2


-2  

As far as I can see the regular expression seems to be valid and should give the results shown in your matching table, according to the XSD specification. Using Microsoft .NET Framework 2 with a test case using your path values and your regular expression I get exactly your expected results.

据我所知,正则表达式似乎是有效的,并且应该根据XSD规范给出匹配表中显示的结果。使用Microsoft .NET Framework 2和测试用例使用您的路径值和正则表达式,我得到了您期望的结果。

So, possible reasons for what you are seeing:

所以,你所看到的可能原因:

  • There is a bug in the XSD implementation you are using.
  • 您正在使用的XSD实施中存在一个错误。
  • Your code isn't validating what you think it is validating (though this seems unlikely given the error message you are seeing)
  • 您的代码未验证您认为它正在验证的内容(尽管根据您看到的错误消息,这似乎不太可能)

If you tell us what implementation you are using and post your code, it may be possible to help further.

如果您告诉我们您正在使用的实施方式并发布您的代码,则可以进一步提供帮助。