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

Chr*_*wes 0 regex xsd

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

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

类型PathModel的定义定义为simpleType如下面的代码段所示.它用于<path>this/is/a/simple/node-path</path>

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

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

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)
Run Code Online (Sandbox Code Playgroud)

出了什么问题?谢谢

Jir*_*era 5

删除^主角.


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

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


this/is/a/simple/node-path

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

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

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