我是xml的新手并尝试使用相应的xsd验证xml但面临错误:
cvc-complex-type.4:属性'公司名称'必须出现在元素上 '公司'
即使我已经在xml和xsd中添加了Companyname。可能导致此错误的原因。任何帮助/建议?
XML:
<?xml version="1.0" encoding="utf-8"?>
<company Companyname="abc" address="hj" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:SchemaLocation="companyxsd.xsd" >
<department dname="dfsdf" deptphoneno="1232321" deptfaxno="2323" deptemail="fefsf">
<employee empid="fsd" ename="fuehuf" emailid="fhueh" phoneno="364764"> </employee>
<employee empid="fsd" ename="fuehuf" emailid="fhueh" phoneno="364764"> </employee>
<contractEmployee name="fdsf" phoneno="12345"> </contractEmployee>
</department>
</company>
companyxsd.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:element name="company" type="comType"/>
<xs:complexType name="comType">
<xs:sequence>
<xs:element name="department" type="deptType"/>
</xs:sequence>
<xs:attribute type="xs:string" name="Companyname" use="required"/>
<xs:attribute type="xs:string" name="address" use="required"/>
</xs:complexType>
<xs:complexType name="deptType">
<xs:sequence>
<xs:element name="employee" type="empType" minOccurs="1" maxOccurs="unbounded"/>
<xs:element name="contractEmployee" type="conType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute type="xs:string" name="dname" use="required"/>
<xs:attribute type="xs:integer" name="deptphoneno" use="required"/>
<xs:attribute type="xs:integer" name="deptfaxno" use="required"/>
<xs:attribute type="xs:string" name="deptemail" use="required"/>
</xs:complexType>
<xs:complexType name="empType">
<xs:attribute type="xs:string" name="empid" use="required"/>
<xs:attribute type="xs:string" name="ename" use="required"/>
<xs:attribute type="xs:string" name="emailid" use="required"/>
<xs:attribute type="xs:integer" name="phoneno" use="required"/>
</xs:complexType>
<xs:complexType name="conType">
<xs:attribute type="xs:string" name="name" use="required"/>
<xs:attribute type="xs:integer" name="phoneno" use="required"/>
</xs:complexType>
</xs:schema>
答案 0 :(得分:1)
您的XML包含许多错误。它格式不正确(开始标记与末尾标记不匹配)并且包含许多违反您的架构的行为(employee
和contractEmployee
代码必须为空。)
我已经清理了你的XML:
<?xml version="1.0" encoding="utf-8"?>
<company Companyname="abc" address="hj" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<department dname="dfsdf" deptphoneno="1232321" deptfaxno="2323" deptemail="fefsf">
<employee empid="fsd" ename="fuehuf" emailid="fhueh" phoneno="364764"/>
<employee empid="fsd" ename="fuehuf" emailid="fhueh" phoneno="364764"/>
<contractEmployee name="fdsf" phoneno="12345"/>
</department>
</company>
这可以针对您的架构进行正确验证,例如可以进行测试,例如在this site。
您可能遇到的任何其他问题与执行验证的方式有关,而与XML或XSD内容无关。