问题描述
我有一个 XML 文件和一个 XSD 文件需要验证.当我验证时,它显示以下错误.
I have an XML file and an XSD file to validate. When i validate , it shows following error.
org.xml.sax.SAXParseException: src-element.3: 元素 'UC4' 有两个类型"属性和匿名类型"子项.其中只有一个是允许用于元素.
XML 文件:
<UC4Execution>
<Script>JOB_NAME</Script>
<UC4 Server="UC4.com" Client="123" UserId="123" Password="*****" >
</UC4 >
</UC4Execution>
XSD 文件:
<xs:element name="UC4Execution">
<xs:complexType>
<xs:sequence>
<xs:element name="Script" type="xs:string"/>
<xs:element name="UC4" type="xs:string" minOccurs="0">
<xs:complexType>
<xs:attribute name="Server" type="xs:string" use="required"/>
<xs:attribute name="Client" type="xs:string" use="required"/>
<xs:attribute name="UserId" type="xs:string" use="required"/>
<xs:attribute name="Password" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
可能是什么问题?
推荐答案
问题正是错误信息所说的地方:
The problem is exactly where the error message says it is:
<xs:element name="UC4" type="xs:string" minOccurs="0">
<xs:complexType>
<xs:attribute name="Server" type="xs:string" use="required"/>
<xs:attribute name="Client" type="xs:string" use="required"/>
<xs:attribute name="UserId" type="xs:string" use="required"/>
<xs:attribute name="Password" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
对于同一个 element
,您不能同时拥有 type="xs:string"
和嵌套的 complexType
.
You can't have both type="xs:string"
and a nested complexType
for the same element
.
如果您希望 UC4
元素只有属性而没有嵌套的文本内容,请删除 type
属性
If you want the UC4
element to have just attributes and no nested text content then remove the type
attribute
<xs:element name="UC4" minOccurs="0">
<xs:complexType>
<xs:attribute name="Server" type="xs:string" use="required"/>
<!-- ... -->
如果您希望它具有both 属性和 字符串内容
If you want it to have both attributes and string content
<UC4 Server="UC4.com" Client="123" UserId="123" Password="*****">content</UC4>
那么你需要一个嵌套的 complexType
和 simpleContent
扩展 xs:string
then you need a nested complexType
with simpleContent
that extends xs:string
<xs:element name="UC4" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="Server" type="xs:string" use="required"/>
<xs:attribute name="Client" type="xs:string" use="required"/>
<xs:attribute name="UserId" type="xs:string" use="required"/>
<xs:attribute name="Password" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
这篇关于XML &XSD 验证失败:元素同时具有“类型"属性和“匿名类型"子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!