针对2 XSD验证简单XML - XMLCopyEditor

时间:2015-08-06 01:24:34

标签: xml ubuntu xsd

我自学了XML。我可以使用1 XSD验证XML,但是当我尝试使用2 XSD验证XML时失败。我一直在阅读有关命名空间的信息,以确定"特定标签"但当我把"任何操作员"不起作用。

验证器 xmlcopyeditor (Ubuntu)说:"没有找到元素孩子的声明"

这是一个XML:

    <?xml version="1.0" encoding="UTF-8" ?>
    <persons xmlns="http://www.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.microsoft.com any_part1.xsd http://www.gumball.com any_part2.xsd">
      <person>
        <firstname>Hege</firstname>
        <lastname>Refsnes</lastname>
        <children>
          <childname>Cecilie</childname>
        </children>
      </person>
      <person>
        <firstname>Stale</firstname>
        <lastname>Refsnes</lastname>
      </person>
    </persons>

首先是XSD:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                               targetNamespace="http://www.microsoft.com"
                               xmlns="http://www.w3schools.com"                          elementFormDefault="qualified">

    <xs:element name="persons">
         <xs:complexType>  
         <xs:sequence>
                <xs:element name="person">
                         <xs:complexType>
                                  <xs:sequence>
                                       <xs:element name="firstname" type="xs:string"/>
                                       <xs:element name="lastname" type="xs:string"/>
                                       <xs:any minOccurs="0"/>
                                  </xs:sequence>
                        </xs:complexType>
                </xs:element> 
          </xs:sequence>
        </xs:complexType>
        </xs:element>
    </xs:schema>

第二个Xsd:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.gumball.com"
    xmlns="http://www.w3schools.com"
    elementFormDefault="qualified">

    <xs:element name="children">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="childname" type="xs:string"
          maxOccurs="unbounded"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>

    </xs:schema> 

W3school - AnyOperator: http://www.w3schools.com/schema/schema_complex_any.asp

1 个答案:

答案 0 :(得分:0)

第一个问题是在模式2中声明的元素子元素在命名空间http://www.gumball.com中定义。在XML文件中使用它时,您使用的是默认命名空间(http://www.microsoft.com)。请参阅xmlns:gb属性,然后查看children和childname的gb:前缀。

<?xml version="1.0" encoding="UTF-8" ?>
<!-- Created with Liquid XML 2015 Designer Edition (Trial) 13.1.0.5909 (http://www.liquid-technologies.com) -->
<persons xmlns="http://www.microsoft.com"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:gb="http://www.gumball.com"
         xsi:schemaLocation="http://www.microsoft.com any_part1.xsd http://www.gumball.com any_part2.xsd">
    <person>
        <firstname>Hege</firstname>
        <lastname>Refsnes</lastname>
        <gb:children>
            <gb:childname>Cecilie</gb:childname>
        </gb:children>
    </person>
</persons>

你的第二个问题是你只能有一个人对象。我已经打开了明确的基数指标,因此您可以更清楚地看到这一点。 Explicitly showing cardinality using Liquid XML Studio