问题
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: Element 'UC4' has both a 'type' attribute and a 'anonymous type' child. Only one of these is allowed for an element.
XML File:
<UC4Execution>
        <Script>JOB_NAME</Script>
        <UC4 Server="UC4.com" Client="123" UserId="123" Password="*****" >
        </UC4 >
</UC4Execution>
XSD File :
        <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>
What might be the issue?
回答1:
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>
You can't have both type="xs:string" and a nested complexType for the same element.
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>
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>
来源:https://stackoverflow.com/questions/21043148/xml-xsd-validation-failed-element-has-both-a-type-attribute-and-a-anonymou