XML/XSD故障排除



你好,

我花了最后两天在XSD规范上工作和示例 该XSD将验证的XML代码。但是,我有XSD 创建并未验证XML,因此我从 关于错误的命令行,我无法确切确切 原因。

如果有人能帮助我,我会很感激。

XML看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<peopleCatalogue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="zoznam-osoby.xsd">
<person role="boss" typeOfContact="humanBeing">
    <firstName>Anton</firstName>
    <lastName>Mlaskal</lastName>
    <email typeOfEmail="personal">mlaskalko.antonko@hotmail.com</email>
    <email typeOfEmail="professional">mlaskal.anton@gmail.com</email>
    <phoneNumber typeOfPhone="cell">+421 948 127 337</phoneNumber>
    <phoneNumber typeOfPhone="home">+420 2 48 48 48</phoneNumber>
    <web>www.mlaskal.com</web>
    <adress>
      <streetName>Lubovnikova</streetName>
      <houseNumber>7</houseNumber>
      <postalCode>84107</postalCode>
      <cityName>Bratislava</cityName>
      <country>Slovakia</country>
    </adress>
    <note typeOfNote="action">Please, kill him as soon as possible.</note>
    <note typeOfNote="undercoverInformation">He is the boss of the whole drug cartel we need to eradicate.</note>
</person>
<person role="boss" typeOfContact="IDServicePair">
    <service>Skype</service>
    <ID>Antonius</ID>
</person> </peopleCatalogue>

XSD看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="peopleCatalogue">
    <xsd:complexType>
        <xsd:all>
            <xsd:element name="person">
                <xsd:complexType>    
                    <xsd:attribute name="role" type="xsd:string" default="unknown"/>
                    <xsd:attribute name="typeOfContact">
                        <xsd:simpleType>
                            <xsd:restriction base="xsd:string">
                                <xsd:pattern value="humanBeing|IDServicePair"/>
                            </xsd:restriction>
                        </xsd:simpleType>
                    </xsd:attribute>
                </xsd:complexType>
            </xsd:element>
        </xsd:all>
    </xsd:complexType>    
</xsd:element>
<xsd:complexType name="IDServicePair">
    <xsd:all> 
        <xsd:element name="ID" minOccurs="1" maxOccurs="1">
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:pattern value="([a-zA-Z0-9 ])*"/>
                </xsd:restriction>
            </xsd:simpleType>
        </xsd:element>
        <xsd:element name="service" minOccurs="1" maxOccurs="1">
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:pattern value="([a-zA-Z0-9 ~!@#$%^*])*"/>
                </xsd:restriction>
            </xsd:simpleType>
        </xsd:element>
    </xsd:all> 
</xsd:complexType>

<xsd:element name="humanBeing">
    <xsd:complexType>
        <xsd:all>
            <xsd:element name="firstName" minOccurs="1" maxOccurs="1">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:pattern value="([a-zA-Z])*"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="lastName" minOccurs="1" maxOccurs="1">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:pattern value="([a-zA-Z])*"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="email">
                <xsd:complexType>
                    <xsd:simpleContent>
                        <xsd:extension base="emailPattern">  
                            <xsd:attribute name="typeOfEmail" default="personal"/>
                        </xsd:extension>
                    </xsd:simpleContent>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="phoneNumber">
                <xsd:complexType>
                    <xsd:simpleContent>
                        <xsd:extension base="phonePattern">  
                            <xsd:attribute name="typeOfPhone" default="cell"/>
                        </xsd:extension>
                    </xsd:simpleContent>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="web">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:pattern value="(https?://[a-zA-Z]+(.[a-zA-Z]+)*.[a-zA-Z]{2,9})|(www(.[a-zA-Z]+)+((.[a-zA-Z]{2,9}){1}))"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="adress">
                <xsd:complexType>
                    <xsd:all>
                        <xsd:element name="streetName">
                            <xsd:simpleType>
                                <xsd:restriction base="xsd:string">
                                    <xsd:pattern value="([a-zA-Z ])*"/>
                                </xsd:restriction>
                            </xsd:simpleType>    
                        </xsd:element>
                        <xsd:element name="houseNumber">
                            <xsd:simpleType>
                                <xsd:restriction base="xsd:integer">
                                    <xsd:pattern value="([0-9])*"/>
                                    <xsd:minInclusive value="0"/>
                                    <xsd:maxInclusive value="99999"/>
                                </xsd:restriction>
                            </xsd:simpleType>    
                        </xsd:element>
                        <xsd:element name="postalCode">
                            <xsd:simpleType>
                                <xsd:restriction base="xsd:integer">
                                    <xsd:pattern value="([0-9])*"/>
                                    <xsd:minInclusive value="0"/>
                                    <xsd:maxInclusive value="99999"/>
                                </xsd:restriction>
                            </xsd:simpleType>
                        </xsd:element>
                        <xsd:element name="cityName">
                            <xsd:simpleType>
                                <xsd:restriction base="xsd:string">
                                    <xsd:pattern value="([a-zA-Z ])*"/>
                                </xsd:restriction>
                            </xsd:simpleType>
                        </xsd:element>
                        <xsd:element name="country">
                            <xsd:simpleType>
                                <xsd:restriction base="xsd:string">
                                    <xsd:pattern value="([a-zA-Z ])*"/>
                                </xsd:restriction>
                            </xsd:simpleType>
                        </xsd:element>
                    </xsd:all>
                </xsd:complexType>    
            </xsd:element>
            <xsd:element name="note">
                <xsd:complexType>  
                    <xsd:simpleContent>  
                        <xsd:extension base="noteType">  
                            <xsd:attribute name="typeOfNote"/>  
                        </xsd:extension>  
                    </xsd:simpleContent>  
                </xsd:complexType>  
            </xsd:element>    
        </xsd:all>
</xsd:complexType> </xsd:element>

我收到的错误消息,当我尝试使用XSD验证XML文件时 通过

xmllint --noout --schema zoznam-osoby.xsd OnePerson.xml in command line is this:
OnePerson.xml:3: element person: Schemas validity error : Element 'person': Character content is not allowed, because the content type
is empty.
OnePerson.xml:3: element person: Schemas validity error : Element 'person': Element content is not allowed, because the content type is
empty.
OnePerson.xml:22: element person: Schemas validity error : Element 'person': This element is not expected.
OnePerson.xml fails to validate

它告诉我,内容类型是空的,但我不知道,什么 这是关于这是关于的。该怎么做才能使其正常工作?

除了类型定义问题(在下面的示例中修复)外,您通常正在寻找有条件类型的分配定义,从XML Schema Standard的1.1开始定义(https://www.w3).org/tr/xmlschema11-1/#ctypealternative),请参见https://stackoverflow.com/a/27880329。

XML模式V1.1数据:

<?xml version="1.0" encoding="UTF-8"?>
<peopleCatalogue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:noNamespaceSchemaLocation="zoznam-osoby.xsd">
    <person role="boss" typeOfContact="humanBeing">
        <firstName>Anton</firstName>
        <lastName>Mlaskal</lastName>
        <email typeOfEmail="personal">mlaskalko.antonko@hotmail.com</email>
        <email typeOfEmail="professional">mlaskal.anton@gmail.com</email>
        <phoneNumber typeOfPhone="cell">+421 948 127 337</phoneNumber>
        <phoneNumber typeOfPhone="home">+420 2 48 48 48</phoneNumber>
        <web>www.mlaskal.com</web>
        <adress>
            <streetName>Lubovnikova</streetName>
            <houseNumber>7</houseNumber>
            <postalCode>84107</postalCode>
            <cityName>Bratislava</cityName>
            <country>Slovakia</country>
        </adress>
        <note typeOfNote="action">Please, kill him as soon as possible.</note>
        <note typeOfNote="undercoverInformation">He is the boss of the whole drug cartel we need to eradicate.</note>
    </person>
    <person role="boss" typeOfContact="IDServicePair">
        <service>Skype</service>
        <ID>Antonius</ID>
    </person> 
</peopleCatalogue>

XML模式V1.1定义:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
            vc:minVersion="1.1">
    <xsd:element name="peopleCatalogue">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="person" minOccurs="0" maxOccurs="unbounded">
                    <xsd:alternative test="@typeOfContact = humanBeing" type="humanBeing"/>        
                    <xsd:alternative test="@typeOfContact = IDServicePair" type="IDServicePair"/>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:complexType name="IDServicePair">
        <xsd:all>
            <xsd:element name="ID" minOccurs="1" maxOccurs="1">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:pattern value="([a-zA-Z0-9 ])*"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="service" minOccurs="1" maxOccurs="1">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:pattern value="([a-zA-Z0-9 ~!@#$%^*])*"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
        </xsd:all>
        <xsd:attribute name="role" type="xsd:string" default="unknown"/>
        <xsd:attribute name="typeOfContact" type="xsd:string" fixed="IDServicePair"/>
    </xsd:complexType>
    <xsd:complexType name="humanBeing">
        <xsd:sequence>
            <xsd:element name="firstName" minOccurs="1" maxOccurs="1">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:pattern value="([a-zA-Z])*"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="lastName" minOccurs="1" maxOccurs="1">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:pattern value="([a-zA-Z])*"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="email" maxOccurs="unbounded">
                <xsd:complexType>
                    <xsd:simpleContent>
                        <xsd:extension base="emailPattern">  
                            <xsd:attribute name="typeOfEmail" default="personal"/>
                        </xsd:extension>
                    </xsd:simpleContent>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="phoneNumber" maxOccurs="unbounded">
                <xsd:complexType>
                    <xsd:simpleContent>
                        <xsd:extension base="phonePattern">  
                            <xsd:attribute name="typeOfPhone" default="cell"/>
                        </xsd:extension>
                    </xsd:simpleContent>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="web">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:pattern value="(https?://[a-zA-Z]+(.[a-zA-Z]+)*.[a-zA-Z]{2,9})|(www(.[a-zA-Z]+)+((.[a-zA-Z]{2,9}){1}))"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="adress">
                <xsd:complexType>
                    <xsd:all>
                        <xsd:element name="streetName">
                            <xsd:simpleType>
                                <xsd:restriction base="xsd:string">
                                    <xsd:pattern value="([a-zA-Z ])*"/>
                                </xsd:restriction>
                            </xsd:simpleType>    
                        </xsd:element>
                        <xsd:element name="houseNumber">
                            <xsd:simpleType>
                                <xsd:restriction base="xsd:integer">
                                    <xsd:pattern value="([0-9])*"/>
                                    <xsd:minInclusive value="0"/>
                                    <xsd:maxInclusive value="99999"/>
                                </xsd:restriction>
                            </xsd:simpleType>    
                        </xsd:element>
                        <xsd:element name="postalCode">
                            <xsd:simpleType>
                                <xsd:restriction base="xsd:integer">
                                    <xsd:pattern value="([0-9])*"/>
                                    <xsd:minInclusive value="0"/>
                                    <xsd:maxInclusive value="99999"/>
                                </xsd:restriction>
                            </xsd:simpleType>
                        </xsd:element>
                        <xsd:element name="cityName">
                            <xsd:simpleType>
                                <xsd:restriction base="xsd:string">
                                    <xsd:pattern value="([a-zA-Z ])*"/>
                                </xsd:restriction>
                            </xsd:simpleType>
                        </xsd:element>
                        <xsd:element name="country">
                            <xsd:simpleType>
                                <xsd:restriction base="xsd:string">
                                    <xsd:pattern value="([a-zA-Z ])*"/>
                                </xsd:restriction>
                            </xsd:simpleType>
                        </xsd:element>
                    </xsd:all>
                </xsd:complexType>    
            </xsd:element>
            <xsd:element name="note" maxOccurs="unbounded">
                <xsd:complexType>  
                    <xsd:simpleContent>  
                        <xsd:extension base="noteType">  
                            <xsd:attribute name="typeOfNote"/>  
                        </xsd:extension>  
                    </xsd:simpleContent>  
                </xsd:complexType>  
            </xsd:element>    
        </xsd:sequence>
        <xsd:attribute name="role" type="xsd:string" default="unknown"/>
        <xsd:attribute name="typeOfContact" type="xsd:string" fixed="humanBeing"/>
    </xsd:complexType>
    <xsd:simpleType name="emailPattern">
        <xsd:restriction base="xsd:string">
            <xsd:pattern value=".+@.+"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="phonePattern">
        <xsd:restriction base="xsd:string">
            <xsd:pattern value="+[ds]+"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="noteType">
        <xsd:restriction base="xsd:string"/>
    </xsd:simpleType>
</xsd:schema>

但是,我不确定XML模式的V1.1的广泛支持如何(至少Netbeans 8.2未能理解它)。因此,我建议使用XML架构V1.0定义将元素名称本身区分元素类型,然后通过所需属性的值来区分元素类型。我认为这样做的意义不大 - 为什么当所需属性完全更改其允许的内容时,为什么使用相同的元素名称?

XML模式V1.0数据:

<?xml version="1.0" encoding="UTF-8"?>
<peopleCatalogue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:noNamespaceSchemaLocation="zoznam-osoby.xsd">
    <humanBeing role="boss">
        <firstName>Anton</firstName>
        <lastName>Mlaskal</lastName>
        <email typeOfEmail="personal">mlaskalko.antonko@hotmail.com</email>
        <email typeOfEmail="professional">mlaskal.anton@gmail.com</email>
        <phoneNumber typeOfPhone="cell">+421 948 127 337</phoneNumber>
        <phoneNumber typeOfPhone="home">+420 2 48 48 48</phoneNumber>
        <web>www.mlaskal.com</web>
        <adress>
            <streetName>Lubovnikova</streetName>
            <houseNumber>7</houseNumber>
            <postalCode>84107</postalCode>
            <cityName>Bratislava</cityName>
            <country>Slovakia</country>
        </adress>
        <note typeOfNote="action">Please, kill him as soon as possible.</note>
        <note typeOfNote="undercoverInformation">He is the boss of the whole drug cartel we need to eradicate.</note>
    </humanBeing>
    <IDServicePair role="boss">
        <service>Skype</service>
        <ID>Antonius</ID>
    </IDServicePair> 
</peopleCatalogue>

XML模式V1.0定义:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="peopleCatalogue">
        <xsd:complexType>
            <xsd:choice minOccurs="0" maxOccurs="unbounded">
                <xsd:element name="humanBeing" type="humanBeing"/>
                <xsd:element name="IDServicePair" type="IDServicePair"/>
            </xsd:choice>
        </xsd:complexType>
    </xsd:element>
    <xsd:complexType name="IDServicePair">
        <xsd:all>
            <xsd:element name="ID" minOccurs="1" maxOccurs="1">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:pattern value="([a-zA-Z0-9 ])*"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="service" minOccurs="1" maxOccurs="1">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:pattern value="([a-zA-Z0-9 ~!@#$%^*])*"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
        </xsd:all>
        <xsd:attribute name="role" type="xsd:string" default="unknown"/>
    </xsd:complexType>
    <xsd:complexType name="humanBeing">
        <xsd:sequence>
            <xsd:element name="firstName" minOccurs="1" maxOccurs="1">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:pattern value="([a-zA-Z])*"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="lastName" minOccurs="1" maxOccurs="1">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:pattern value="([a-zA-Z])*"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="email" maxOccurs="unbounded">
                <xsd:complexType>
                    <xsd:simpleContent>
                        <xsd:extension base="emailPattern">  
                            <xsd:attribute name="typeOfEmail" default="personal"/>
                        </xsd:extension>
                    </xsd:simpleContent>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="phoneNumber" maxOccurs="unbounded">
                <xsd:complexType>
                    <xsd:simpleContent>
                        <xsd:extension base="phonePattern">  
                            <xsd:attribute name="typeOfPhone" default="cell"/>
                        </xsd:extension>
                    </xsd:simpleContent>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="web">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:pattern value="(https?://[a-zA-Z]+(.[a-zA-Z]+)*.[a-zA-Z]{2,9})|(www(.[a-zA-Z]+)+((.[a-zA-Z]{2,9}){1}))"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="adress">
                <xsd:complexType>
                    <xsd:all>
                        <xsd:element name="streetName">
                            <xsd:simpleType>
                                <xsd:restriction base="xsd:string">
                                    <xsd:pattern value="([a-zA-Z ])*"/>
                                </xsd:restriction>
                            </xsd:simpleType>    
                        </xsd:element>
                        <xsd:element name="houseNumber">
                            <xsd:simpleType>
                                <xsd:restriction base="xsd:integer">
                                    <xsd:pattern value="([0-9])*"/>
                                    <xsd:minInclusive value="0"/>
                                    <xsd:maxInclusive value="99999"/>
                                </xsd:restriction>
                            </xsd:simpleType>    
                        </xsd:element>
                        <xsd:element name="postalCode">
                            <xsd:simpleType>
                                <xsd:restriction base="xsd:integer">
                                    <xsd:pattern value="([0-9])*"/>
                                    <xsd:minInclusive value="0"/>
                                    <xsd:maxInclusive value="99999"/>
                                </xsd:restriction>
                            </xsd:simpleType>
                        </xsd:element>
                        <xsd:element name="cityName">
                            <xsd:simpleType>
                                <xsd:restriction base="xsd:string">
                                    <xsd:pattern value="([a-zA-Z ])*"/>
                                </xsd:restriction>
                            </xsd:simpleType>
                        </xsd:element>
                        <xsd:element name="country">
                            <xsd:simpleType>
                                <xsd:restriction base="xsd:string">
                                    <xsd:pattern value="([a-zA-Z ])*"/>
                                </xsd:restriction>
                            </xsd:simpleType>
                        </xsd:element>
                    </xsd:all>
                </xsd:complexType>    
            </xsd:element>
            <xsd:element name="note" maxOccurs="unbounded">
                <xsd:complexType>  
                    <xsd:simpleContent>  
                        <xsd:extension base="noteType">  
                            <xsd:attribute name="typeOfNote"/>  
                        </xsd:extension>  
                    </xsd:simpleContent>  
                </xsd:complexType>  
            </xsd:element>    
        </xsd:sequence>
        <xsd:attribute name="role" type="xsd:string" default="unknown"/>
    </xsd:complexType>
    <xsd:simpleType name="emailPattern">
        <xsd:restriction base="xsd:string">
            <xsd:pattern value=".+@.+"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="phonePattern">
        <xsd:restriction base="xsd:string">
            <xsd:pattern value="+[ds]+"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="noteType">
        <xsd:restriction base="xsd:string"/>
    </xsd:simpleType>
</xsd:schema>

好的,经过大量努力,我解决了它。

基本上,我试图做的是在XML模式中指定某种语句。如果一个人具有typeofContact = Humanbeing,我希望它具有一组元素,如果它为typeofContact = idServicePair,我希望它只有两个元素(ID Service)。

我认为,如果将属性指定为复杂型,这是可能的。但是,这不是,经过数小时的努力,您最终会遇到我最终遇到的同样的错误,人们似乎在回答,但基本上只是像孩子一样与您交谈。

所以我只是删除了很多工作,只是处理了我无法做到的事实。

相关内容

  • 没有找到相关文章

最新更新