我有一个xml文件,它有一些日期值和其他数据类型.
<Purchasedate Name="purcaseDate" value=""/>
Run Code Online (Sandbox Code Playgroud)
我正在使用xsd文件验证这些xml文件.在xsd shcema中,我为dd/mm/yyyy格式编写了正则表达式模式.
如果value属性具有值,则此工作正常.我的模式正在验证value属性.
字段(purchasedate)不是必需的.如果value ="",这意味着我的模式也在验证空字符串,这不是强制性的.
我需要验证可选字段,我<xs:attribute name="PurchaseDate" use="optional">也在使用.
当值标记不为空时,我需要验证此字段.
那太容易了..
您所要做的就是在您的帐户中包含空字符串规范pattern
这是这样做的方式..
<xs:pattern value="|(Regular_pattern_goes_here)"/>
为了您的参考,我已经编写了一些代码块......只需通过它们即可...
示例XML:
<?xml version="1.0" encoding="utf-8"?>
<xmln xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com XMLFile1.xsd" xmlns="http://www.xsdef.com/xml/123">
<Purchasedate Name="purcaseDate" value=""/>
</xmln>
Run Code Online (Sandbox Code Playgroud)
示例XSD :(包括自定义类型def)
<xs:schema xmlns:xsLocal="http://www.xsdef.com/xml/123" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.xsdef.com/xml/123" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="xmln">
<xs:complexType>
<xs:sequence>
<xs:element name="Purchasedate">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="Name" type="xs:string" use="required" />
<xs:attribute name="value" type="xsLocal:CUSTOM_DATE" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="CUSTOM_DATE">
<xs:restriction base="xs:string">
<xs:pattern value="|((01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)/(01|02|03|04|05|06|07|08|09|10|11|12)/[1-2][0-9][0-9][0-9])"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
尝试将此属性 nillable="true" 添加到 xml 标记定义中您也可以查看他的链接http://www.zvon.org/xxl/XMLSchemaTutorial/Output/ser_over_st0.html
Best Reagds,
Iordan