[英]LibXML2 date validation errors with regular expression
在针对使用正则表达式的架构验证XML时遇到问题。 我正在使用LibXML。 实际上,libXML由PHP使用,但是在控制台中当然也会出现相同的问题。 XML应该是有效的,因为我已经使用一些在线验证器对其进行了测试。
无论如何要解决? 修改架构不是一个很好的选择,因为它是由第三方提供的。
我将xmllint命令与libXML版本2.7.8和2.9.1一起使用。
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
我的XML文件“ x”是:
<?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次,因为默认情况下重复一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.