【问题标题】:XML & XSD validation failed: Element has both a 'type' attribute and a 'anonymous type' childXML 和 XSD 验证失败:元素同时具有“类型”属性和“匿名类型”子项
【发布时间】:2014-01-29 08:34:23
【问题描述】:

我有一个 XML 文件和一个 XSD 文件要验证。当我验证时,它显示以下错误。

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>

可能是什么问题?

【问题讨论】:

    标签: xml xslt xml-parsing xsd


    【解决方案1】:

    问题正是错误消息所说的:

    <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

    如果您希望UC4 元素只有属性而没有嵌套文本内容,则删除type 属性

    <xs:element name="UC4" minOccurs="0">
      <xs:complexType>
        <xs:attribute name="Server" type="xs:string" use="required"/>
        <!-- ... -->
    

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

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

    那么你需要一个嵌套的complexTypesimpleContent 扩展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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多