你好,
我花了两天时间研究XSD规范和示例 此XSD将验证的XML代码。但是,我有XSD created不会验证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
它告诉我,内容类型是空的,但我不知道,是什么 这是关于的内容。如何使其发挥作用?
答案 0 :(得分:1)
除了类型定义问题(在下面的示例中修复)之外,您通常会查找从XML Schema标准(https://www.w3.org/TR/xmlschema11-1/#cTypeAlternative)的1.1版开始定义的条件类型分配,请参阅https://stackoverflow.com/a/27880329。< / p>
XML Schema 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 Schema 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="\+[\d\s]+"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="noteType">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
</xsd:schema>
但是,我不确定XML Schema v1.1的广泛支持是什么(至少NetBeans 8.2无法理解它)。因此,我建议使用XML Schema v1.0定义区分元素类型,而不是元素名称本身,而不是必需属性的值。我这样做没有多大意义 - 当必需属性完全改变其允许的内容时,为什么要使用相同的元素名称?
XML Schema 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 Schema 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="\+[\d\s]+"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="noteType">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
</xsd:schema>
答案 1 :(得分:0)
好的,经过很多努力,我解决了它。
基本上,我尝试做的是在XML模式中指定某种IF语句。如果一个人有一个typeOfContact = humanBeing,我希望它有一组元素,如果它是typeOfContact = IDServicePair,我希望它只有两个元素(ID + service)。
我认为如果我将属性指定为complexType,那么这是可能的。然而,经过几个小时的努力,你最终会遇到同样的错误,人们似乎在回答,但基本上只是像个孩子一样和你说话。
所以我刚刚删除了很多工作,只是处理了这个事实,我不能那样做。