xml:元素的值无效

时间:2017-03-15 07:39:03

标签: java xml xsd

我使用eclipse工具创建了xsd文件,我也在同一工具中使用该xsd文件创建了xml文件。我无法识别这个错误。

cvc-type.3.1.3: The value 'tns:name' of element 'tns:name' is not valid.

cvc-type.3.1.3: The value 'tns:description' of element 'tns:description' is not valid.

根据我的分析,它很好,并且遵循xsd文件。

这是我的xsd文件:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://www.jaggorder.org/order"
  xmlns:tns="http://www.jaggorder.org/order"
   elementFormDefault="qualified">

   <element name="order" type="tns:order"></element>
   <complexType name="order">
   <sequence>
   <element name= "product" type = "string"></element>
    <sequence>
   <element name= "name" type = "tns:name_rs"></element>
    <element name= "description" type = "tns:desp"></element>
     <element name= "price" type = "int"></element>
      <element name= "category" type = "string"></element>

  </sequence>
   </sequence>
 </complexType>

  <simpleType name="name_rs">
  <restriction base="string">
  <length value="20"></length></restriction>
  </simpleType>

  <simpleType name="desp">
  <restriction base="string">
  <length value="100"></length></restriction>
  </simpleType>

   <simpleType name="category_en">
  <restriction base="string">
  <enumeration value="electronics"></enumeration>
  <enumeration value="Books"></enumeration>
  <enumeration value="shoes"></enumeration>
  </restriction>
  </simpleType>



</schema>

这是我从xsd文件生成的xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<tns:order xmlns:tns="http://www.jaggorder.org/order" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.jaggorder.org/order order.xsd ">
  <tns:product>tns:product</tns:product>
  <tns:name>tns:name</tns:name>
  <tns:description>tns:description</tns:description>
  <tns:price>0</tns:price>
  <tns:category>tns:category</tns:category>
</tns:order>

1 个答案:

答案 0 :(得分:0)

发现错误,我花了好几个小时来纠正,但最终还是修复了它。

这里是正确的xsd: -

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://www.jaggorder.org/order"
  xmlns:tns="http://www.jaggorder.org/order"
   elementFormDefault="qualified">

   <element name="order" type="tns:order"></element>
   <complexType name="order">
   <sequence>
   <element name= "product" >
   <complexType>
    <sequence>
   <element name= "name" type = "tns:name_rs"></element>
    <element name= "description" type = "tns:desp"></element>
     <element name= "price" type = "int"></element>
      <element name= "category" type = "tns:category_en"></element>
  </sequence>
  </complexType>
  </element>
   </sequence>
 </complexType>

  <simpleType name="name_rs">
  <restriction base="string">
  <maxLength value="20"></maxLength></restriction>
  </simpleType>

  <simpleType name="desp">
  <restriction base="string">
  <maxLength value="100"></maxLength></restriction>
  </simpleType>

   <simpleType name="category_en">
  <restriction base="string">
  <enumeration value="electronics"></enumeration>
  <enumeration value="Books"></enumeration>
  <enumeration value="shoes"></enumeration>
  </restriction>
  </simpleType>



</schema>

将simpleType-lenght更改为MaxLength,并将product元素放置在适当的层次结构中。

所以结果xml会像这样没有错误: -

<?xml version="1.0" encoding="UTF-8"?>
<tns:order xmlns:tns="http://www.jaggorder.org/order" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jaggorder.org/order order.xsd ">
  <tns:product>
    <tns:name>tns:name</tns:name>
    <tns:description>description</tns:description>
    <tns:price>0</tns:price>
    <tns:category>Books</tns:category>
  </tns:product>
</tns:order>

感谢所有观众:)