如何验证xsd中的空字符串值标记
我有一个xml文件,其中包含一些日期值和其他数据类型如何验证xsd中的空字符串值标记,xsd,Xsd,我有一个xml文件,其中包含一些日期值和其他数据类型 <Purchasedate Name="purcaseDate" value=""/> 我正在用xsd文件验证这些xml文件。 在xsd shcema中,我为dd/mm/yyyy格式编写了一个正则表达式模式 如果value属性有一个值,这就可以正常工作。 我的模式正在根据value属性进行验证 字段(purchasedate)不是必填字段。 如果value=“”,这意味着我的模式也在根据空字符串进行验证,这不是强制性的 我需
<Purchasedate Name="purcaseDate" value=""/>
我正在用xsd文件验证这些xml文件。
在xsd shcema中,我为dd/mm/yyyy
格式编写了一个正则表达式模式
如果value属性有一个值,这就可以正常工作。
我的模式正在根据value属性进行验证
字段(purchasedate
)不是必填字段。
如果value=“”,这意味着我的模式也在根据空字符串进行验证,这不是强制性的
我需要验证可选字段
我也在使用
当值标记不为空时,我需要验证此字段。尝试将此属性nillable=“true”添加到xml标记定义中 你也可以看看他的链接
最好的理由,
伊尔丹那太容易了 只需在
模式中包含空字符串规范
这是做那件事的方法。。
为供参考,我编写了一个代码块示例。。只要检查一下
示例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>
示例XSD:(包括自定义类型def)
正则表达式中的“?”字符表示前面的字符必须出现0或1次
因此,为了解决您的问题,您需要将正则表达式括在括号中,并在末尾打上问号:
<xs:simpleType name="PurchaseDateType">
<xs:restriction base="xs:string">
<xs:pattern value="(Regular_pattern_goes_here)?"/>
</xs:restriction>
</xs:simpleType>
在你的字段上使用这个类型,你应该很好,
如果你控制XML的语法,你应该考虑如下定义元素。因为XMLSchema已经提供了日期类型,所以除非有很好的理由,否则应该使用它。我之所以这样说,是因为这将使其他人更容易使用xml,也使您以后更容易使用更好的代码框架。我没有包括“name”属性,因为它对于元素名来说似乎是多余的
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="Purchasedate" nillable="true" type="xs:date"/>
<xs:element name="Purchasedate2">
<xs:complexType>
<xs:attribute name="value" type="xs:date"/>
</xs:complexType>
</xs:element>
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="Purchasedate"/>
<xs:element minOccurs="0" ref="Purchasedate2"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="Purchasedate" nillable="true" type="xs:date"/>
<xs:element name="Purchasedate2">
<xs:complexType>
<xs:attribute name="value" type="xs:date"/>
</xs:complexType>
</xs:element>
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="Purchasedate"/>
<xs:element minOccurs="0" ref="Purchasedate2"/>
</xs:sequence>
</xs:complexType>
</xs:element>