簡體   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