XML文档中遇到未知元素错误

时间:2012-02-24 07:04:12

标签: xml xsd

我正在尝试针对XML Schema验证XML文档。 我得到的错误是未知元素'jobinfo',它存在于根目录 层次结构。元素'jobinfo'在XML Schema中。我是这个地区的新手。任何人都可以帮忙吗?

这是我的XML

<?xml version="1.0" encoding="utf-8"?>
<saw:jobInfo xmlns:saw="com.analytics.web/report/v1.1">
   <saw:jobStats>...........</saw:jobStats>
   <saw:detailedInfo> .....</saw:detailedInfo>
   <saw:fileInfo>..........</saw:fileInfo>
</saw:jobInfo>

这是我的XSD

 <?xml version="1.0" encoding="utf-8" ?>
 <xs:schema targetNamespace="com.analytics.web/report/v1.1"  
   xmlns="com.analytics.web/report/v1.1" xmlns:saw="com.analytics.web/report/v1.1" 
   xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" 
   attributeFormDefault="unqualified">

 <xs:element name="jobInfo" type="jobInfo"/>
   <xs:complexType name="jobInfo">
      <xs:sequence>
         <xs:element name="jobStats" type="jobStats" minOccurs="1" maxOccurs="1"/>
         <xs:element name="detailedInfo"  type="detailedInfo" minOccurs="0" maxOccurs="1"/>
         <xs:element name="fileInfo"  type="fileInfo" minOccurs="0"/>
      </xs:sequence>
   </xs:complexType>

    < other elements>

1 个答案:

答案 0 :(得分:0)

元素类型jobStatsdetailedInfofileInfo的问题。

由于未定义这些类型。

我已经用这种方式修改了 XSD : -

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="com.analytics.web/report/v1.1" xmlns="com.analytics.web/report/v1.1" xmlns:saw="com.analytics.web/report/v1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="jobInfo" type="jobInfo"/>
    <xs:complexType name="jobInfo">
        <xs:sequence>
            <xs:element name="jobStats" type="xs:string" minOccurs="1" maxOccurs="1"/>
            <xs:element name="detailedInfo" type="xs:string" minOccurs="0" maxOccurs="1"/>
            <xs:element name="fileInfo" type="xs:string" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

OUTPUT示例XML格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<saw:jobInfo xsi:schemaLocation="com.analytics.web/report/v1.1 Untitled1.xsd" xmlns:saw="com.analytics.web/report/v1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <saw:jobStats>String</saw:jobStats>
    <saw:detailedInfo>String</saw:detailedInfo>
    <saw:fileInfo>String</saw:fileInfo>
</saw:jobInfo>

您应该在XML上指定xsi:schemaLocation,同时使用XSD进行验证。

xsi:schemaLocation="yourXSDfilepath"

<saw:jobInfo xsi:schemaLocation="yourXSDfilepath"  xmlns:saw="com.analytics.web/report/v1.1">