以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DTD/XML Schema 』  (http://bbs.xml.org.cn/list.asp?boardid=23)
----  对复杂型xm的schema定义,元素出错问题请求帮助。l  (http://bbs.xml.org.cn/dispbbs.asp?boardid=23&rootid=&id=52460)


--  作者:gaojianxun000
--  发布时间:9/10/2007 10:28:00 AM

--  对复杂型xm的schema定义,元素出错问题请求帮助。l
我copy了个xml如下:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="false.xsl"?>
<persons xmlns="http://www.w3school.com"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:schemaLocation="http://www.w3school.com false.xsd">
<person>
      <name>lisongtao</name>
      <age>25</age>
      <tel>
       <hometel>
           <tell>64456787</tell>
           <childtel>64456785</childtel>
        </hometel>
        <offtel>44442222</offtel>
        <worktel>3333333</worktel>
      </tel>
</person>

<person>
   <name>liweiasd</name>
      <age>25</age>
      <tel>
       <hometel>
            <tell>66545434</tell>
            <childtel>65433333</childtel>
       </hometel>
          <offtel>64442222</offtel>
          <worktel>6333333</worktel>
      </tel>
</person>

<person>
      <name>zhangxue</name>
      <age>25</age>
      <tel>
       <hometel>
           <tell>84343434</tell>
           <childtel>8333333</childtel>
       </hometel>
          <offtel>84442222</offtel>
          <worktel>8333333</worktel>
      </tel>
</person>
<person>
      <name>wureasd</name>
      <age>26</age>
      <tel>
         <hometel>
            <tell>66545434</tell>
            <childtel>65433333</childtel>
       </hometel>
          <offtel>64442222</offtel>
          <worktel>6333333</worktel>
      </tel>
</person>  
</persons>

对于上面的xml我写了这样一个schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns="http://www.w3school.com"
 targetNamespace="http://www.w3school.com"
 elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="persons">
<xs:complexType>
    <xs:sequence>
 <xs:element name="person">
  <xs:complexType>
   <xs:sequence>
  <xs:element name="name" type="xs:string"/>
  <xs:element name="age" type="xs:string"/>
  <xs:element ref="tel"/>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
     </xs:sequence>
</xs:complexType>
</xs:element>
 
<xs:element name="tel">
<xs:complexType>
 <xs:sequence>
  <xs:element name="hometel">
   <xs:complexType>
   <xs:sequence>
       <xs:element name="tell" type="xs:string"/>
       <xs:element name="childtel" type="xs:string"/>
   </xs:sequence>
   </xs:complexType>
  </xs:element>
  <xs:element name="offtel" type="xs:string"/>
  <xs:element name="worktel" type="xs:string"/>
 </xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

结果在xml中的第二个person老是显示错误,错误类型为:   cvc-complex-type.2.4.d: Invalid content was found starting with element 'person'. No child
element is expected at this point.

想了很长时间不知道所谓.希望高手帮忙。


--  作者:PrimoJin
--  发布时间:10/11/2007 8:59:00 AM

--  
那是因为你在XML Schema 中的根元素里面没有指定XML文档的元素数量。
你需要把原有的XML Schema中的根元素更改如下就不会报错误了。
<xs:element name="persons">
<xs:complexType>
    <xs:sequence>
<xs:element name="person" maxOccurs="unbounded">
  <xs:complexType>
   <xs:sequence>
  <xs:element name="name" type="xs:string"/>
  <xs:element name="age" type="xs:string"/>
  <xs:element ref="xbe2:tel"/>
   </xs:sequence>
  </xs:complexType>
</xs:element>

这个问题的原因是:
1.你没有指定为可控制重复性,使用minOccurs和maxOccurs属性(它们不仅对元素声明有效,而且对sequence、choice、all也是有效的)。它们的默认值都是1,表示元素必需出现且只能出现一次(可以自己定义整数形式)。
2.还有两个特殊的:当minOccurs为0时,该元素是可选的。当maxOccurs等于关键字unbounded时,该元素可以无限重复。
备注:提问有一个月了。也许楼主已经把问题都解决了。o(∩_∩)o...


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
3,246.094ms