如何在不指定XML模式中的顺序的情况下验证子元素是否存在?
我正在尝试开发一个XML模式文件来验证如何在不指定XML模式中的顺序的情况下验证子元素是否存在?,xml,xsd,xsd-1.0,Xml,Xsd,Xsd 1.0,我正在尝试开发一个XML模式文件来验证Person元素。我们的应用程序要求一个人有名和姓,但不关心他们的顺序。它还允许将其他元素放在Person元素下。因此,以下各项是有效的: <person> <firstName>Jon</firstName> <lastName>Smith</lastName> </person> <person> <lastName>Smith<
Person
元素。我们的应用程序要求一个人有名
和姓
,但不关心他们的顺序。它还允许将其他元素放在Person
元素下。因此,以下各项是有效的:
<person>
<firstName>Jon</firstName>
<lastName>Smith</lastName>
</person>
<person>
<lastName>Smith</lastName>
<firstName>Jon</firstName>
</person>
<person>
<title>Mr</title>
<firstName>Jon</firstName>
<lastName>Smith</lastName>
</person>
<person>
<title>Mr</title>
<lastName>Smith</lastName>
<firstName>Jon</firstName>
<suffix>CEng</suffix>
</person>
<person>
<title>Mr</title>
<lastName>Smith</lastName>
<middleInitial>G</middleInitial>
<firstName>Jon</firstName>
<suffix>CEng</suffix>
</person>
我尝试创建一个复杂类型,如下所示:
<xsd:element name="person">
<xsd:complexType>
<xsd:all>
<xsd:element minOccurs="1" maxOccurs="1" name="firstName" />
<xsd:element minOccurs="1" maxOccurs="1" name="lastName"/>
<xsd:any minOccurs="0" maxOccurs="unbounded" processContents="skip" />
</xsd:all>
</xsd:complexType>
</xsd:element>
但是显然
any
不允许在all
中使用。是否可以获取XML模式来执行此验证?如果是,如何进行?如果您有XML Schema 1.1验证程序:
<xsd:element name="person">
<xsd:complexType>
<xsd:sequence>
<xsd:any minOccurs="0" maxOccurs="unbounded" processContents="skip" />
</xsd:sequence>
</xsd:complexType>
<xsd:assert test="firstName and lastName"/>
</xsd:element>
在xsd 1.1中,xsd:all中包含xsd:any是有效的,但如果未指定的元素与firstName和lastName位于同一命名空间中,它仍然可能导致错误,因为内容模型可能不明确。但是xsd:assert可以工作
在XSD1.0中,可以将元素的所有顺序指定为带有选项的序列。但是,除firstName和lastName之外的元素需要位于不同的名称空间中,否则在模式中会出现“不明确的内容模型”错误。我不确定您是否可以在数据中使用这个“其他名称空间”约束,但我认为这是在XMLSchema版本1.0中对此进行建模的唯一方法
XSD:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="persons">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="person" minOccurs="1" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="firstName" />
<xsd:element name="lastName" />
<xsd:element name="person">
<xsd:complexType>
<xsd:sequence>
<xsd:any minOccurs="0" maxOccurs="unbounded" processContents="skip" namespace="##other" />
<xsd:choice>
<xsd:sequence>
<xsd:element minOccurs="1" maxOccurs="1" ref="firstName" />
<xsd:any minOccurs="0" maxOccurs="unbounded" processContents="skip" namespace="##other" />
<xsd:element minOccurs="1" maxOccurs="1" ref="lastName" />
</xsd:sequence>
<xsd:sequence>
<xsd:element minOccurs="1" maxOccurs="1" ref="lastName" />
<xsd:any minOccurs="0" maxOccurs="unbounded" processContents="skip" namespace="##other" />
<xsd:element minOccurs="1" maxOccurs="1" ref="firstName" />
</xsd:sequence>
</xsd:choice>
<xsd:any minOccurs="0" maxOccurs="unbounded" processContents="skip" namespace="##other" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<?xml version="1.0"?>
<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd" xmlns:o="urn:foobar">
<person>
<firstName>Jon</firstName>
<lastName>Smith</lastName>
</person>
<person>
<lastName>Smith</lastName>
<firstName>Jon</firstName>
</person>
<person>
<o:title>Mr</o:title>
<firstName>Jon</firstName>
<lastName>Smith</lastName>
</person>
<person>
<o:title>Mr</o:title>
<lastName>Smith</lastName>
<firstName>Jon</firstName>
<o:suffix>CEng</o:suffix>
</person>
<person>
<o:title>Mr</o:title>
<lastName>Smith</lastName>
<o:middleInitial>G</o:middleInitial>
<firstName>Jon</firstName>
<o:suffix>CEng</o:suffix>
</person>
</persons>
输入XML:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="persons">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="person" minOccurs="1" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="firstName" />
<xsd:element name="lastName" />
<xsd:element name="person">
<xsd:complexType>
<xsd:sequence>
<xsd:any minOccurs="0" maxOccurs="unbounded" processContents="skip" namespace="##other" />
<xsd:choice>
<xsd:sequence>
<xsd:element minOccurs="1" maxOccurs="1" ref="firstName" />
<xsd:any minOccurs="0" maxOccurs="unbounded" processContents="skip" namespace="##other" />
<xsd:element minOccurs="1" maxOccurs="1" ref="lastName" />
</xsd:sequence>
<xsd:sequence>
<xsd:element minOccurs="1" maxOccurs="1" ref="lastName" />
<xsd:any minOccurs="0" maxOccurs="unbounded" processContents="skip" namespace="##other" />
<xsd:element minOccurs="1" maxOccurs="1" ref="firstName" />
</xsd:sequence>
</xsd:choice>
<xsd:any minOccurs="0" maxOccurs="unbounded" processContents="skip" namespace="##other" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<?xml version="1.0"?>
<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd" xmlns:o="urn:foobar">
<person>
<firstName>Jon</firstName>
<lastName>Smith</lastName>
</person>
<person>
<lastName>Smith</lastName>
<firstName>Jon</firstName>
</person>
<person>
<o:title>Mr</o:title>
<firstName>Jon</firstName>
<lastName>Smith</lastName>
</person>
<person>
<o:title>Mr</o:title>
<lastName>Smith</lastName>
<firstName>Jon</firstName>
<o:suffix>CEng</o:suffix>
</person>
<person>
<o:title>Mr</o:title>
<lastName>Smith</lastName>
<o:middleInitial>G</o:middleInitial>
<firstName>Jon</firstName>
<o:suffix>CEng</o:suffix>
</person>
</persons>
乔恩
史密斯
史密斯
乔恩
先生
乔恩
史密斯
先生
史密斯
乔恩
曾
先生
史密斯
G
乔恩
曾
我现在使用的是.Net 4.0,可惜不是。除非有人知道第三方库有什么好处。好的,我的答案在1.0中有点扩展。不确定这是否对你有效,但我相信这是你能得到的最接近的。