XML &XSD 验证失败:元素同时具有“类型"属性和“匿名类型"子项 [英] XML & XSD validation failed: Element has both a 'type' attribute and a 'anonymous type' child

查看:17
本文介绍了XML &XSD 验证失败:元素同时具有“类型"属性和“匿名类型"子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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' 有两个类型"属性和匿名类型"子项.其中只有一个是允许一个元素.

org.xml.sax.SAXParseException: src-element.3: Element 'UC4' has both a 'type' attribute and a 'anonymous type' child. Only one of these is allowed for an element.

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"/>
    <!-- ... -->

如果你想让它有两个属性字符串内容

If you want it to have both attributes and string content

<UC4 Server="UC4.com" Client="123" UserId="123" Password="*****">content</UC4>

那么你需要一个嵌套的 complexTypesimpleContent 扩展 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 &amp;XSD 验证失败:元素同时具有“类型"属性和“匿名类型"子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆