根据使用正则表达式的模式验证XML时遇到问题。我使用的是LibXML。实际上,libXML是由PHP使用的,但同样的问题当然也发生在控制台中。XML应该是有效的,因为我用一些在线验证器对它进行了测试。
总之是变通办法吗?修改模式不是一个很好的选择,因为它是由第三方源提供的。
我在libXML 2.7.8和2.9.1版本中使用xmllint命令。
xmllint: using libxml version 20708
compiled with: Threads Tree Output Push Reader Patterns Writer SAXv1 FTP HTTP DTDValid HTML Legacy C14N Catalog XPath XPointer XInclude Iconv ISO8859X Unicode Regexps Automata Expr Schemas Schematron Modules Debug Zlib
xmllint: using libxml version 20901
compiled with: Threads Tree Output Push Reader Patterns Writer SAXv1 FTP HTTP DTDValid HTML Legacy C14N Catalog XPath XPointer XInclude Iconv ISO8859X Unicode Regexps Automata Expr Schemas Schematron Modules Debug Zlib
我使用的命令:
xmllint --noout -schema s x
我得到的错误:
x:2: element testdate: Schemas validity error : Element 'testdate': [facet 'pattern'] The value '31.8.2013' is not accepted by the pattern '((([0-2]{0,1}[0-9]{1})|(3[0,1]{1}))'.((0?[1-9]{1})|(1[0-2]{1}))'.2[0-9]{3})?'.
x:2: element testdate: Schemas validity error : Element 'testdate': '31.8.2013' is not a valid value of the atomic type 'mdatetype'.
x fails to validate
我的名为"x"的XML文件是:
<?xml version="1.0" encoding="UTF-8"?>
<testdate>31.8.2013</testdate>
我的名为"s"的架构是:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="testdate" type="mdatetype"/>
<xs:simpleType name="mdatetype">
<xs:restriction base="xs:string">
<xs:maxLength value="10"/>
<xs:pattern value="((([0-2]{0,1}[0-9]{1})|(3[0,1]{1}))'.((0?[1-9]{1})|(1[0-2]{1}))'.2[0-9]{3})?"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
编辑
有些正则表达式有效。我用芬兰社会安全号码进行了测试
架构
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="x" />
<xs:element name="testdate" type="mdatetype" />
<xs:element name="testhetu" type="mhetu" />
<xs:simpleType name="mdatetype">
<xs:restriction base="xs:string">
<xs:pattern value="((([0-2]{0,1}[0-9]{1})|(3[0,1]{1}))'.((0?[1-9]{1})|(1[0-2]{1}))'.2[0-9]{3})?"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="mhetu">
<xs:restriction base="xs:string">
<xs:pattern value="((([0-2]{1}[0-9]{1})|(3[0,1]{1}))((0[0-9]{1})|(1[0-2]{1}))[0-9]{2}['+'-aA]{1}[0-9]{3}[0-9A-Fa-fHhJ-Nj-nPpR-Yr-y]{1})?"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
XML
<x>
<testhetu>151182-152x</testhetu>
<testhetu>151182A154G</testhetu>
</x>
*结果(只有最新的SSN无效,因为不允许使用G)*
x:3: element testhetu: Schemas validity error : Element 'testhetu': [facet 'pattern'] The value '151182A154G' is not accepted by the pattern '((([0-2]{1}[0-9]{1})|(3[0,1]{1}))((0[0-9]{1})|(1[0-2]{1}))[0-9]{2}['+'-aA]{1}[0-9]{3}[0-9A-Fa-fHhJ-Nj-nPpR-Yr-y]{1})?'.
x:3: element testhetu: Schemas validity error : Element 'testhetu': '151182A154G' is not a valid value of the atomic type 'mhetu'.
x fails to validate
欢迎任何帮助!
看起来libxml不理解重复计数中0到1的表示法,只需用问号替换即可。
所以你在第一个模式中的模式应该是:
((([0-2]?[0-9]{1})|(3[0,1]{1}))'.((0?[1-9]{1})|(1[0-2]{1}))'.2[0-9]{3})?
或者更好的是,你可以将其简化为:
((([0-2]?[0-9])|(3[0,1]))'.((0?[1-9])|(1[0-2]))'.2[0-9]{3})?
这是因为你不需要明确地说这个角色重复1次,因为重复一次是默认的。