繁体   English   中英

如何在不指定XML模式的顺序的情况下验证子元素的存在?

[英]How to validate that child elements exist with out specifying the order in XML Schema?

我正在尝试开发XML模式文件来验证Person元素。 我们的应用程序要求一个人具有一个FirstNameLastName但不在乎它们的顺序。它还允许将其他元素放在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>

但是以下内容无效,因为它没有firstName

<person>
    <title>Mr</title>
    <lastName>Smith</lastName>
    <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不允许有all 是否有可能获取XML Schema进行此验证? 如果可以,怎么办?

如果您具有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:any包含在xsd:all中是有效的,但是如果您未指定的元素与firstName和lastName位于相同的名称空间中,则由于内容模型可能模棱两可,它仍可能会导致错误。 但是xsd:assert可以工作。

在xsd 1.0中,可以将元素可以具有的所有顺序指定为带有选择的序列。 但是,除firstName和lastName之外的其他元素必须位于不同的名称空间中,否则在架构上会收到此“歧义内容模型”错误。 我不确定是否可以在数据中使用此“其他名称空间”约束,但是我认为这是在Xml Schema版本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

<?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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM