0

I have two child elements with the same name but different attributes. Is there a way to write a valid XSD for it.

Error I get now is: Multiple element with name link and of different type appear in the model group

I have an XML like this:

        <entry>
            <id> 123 </id>
            <link rel="alternate" type="text/html" href="ABC.html" />
            <link rel="amphtml" href="https:ABCD.html" >
              <inline:inline type="text/html">
                <![CDATA[
                  <!doctype html>
                  <html amp>

                  </html>
                ]]>
              </inline:inline>
            </link>
        </entry>

I am defining the XSD like this:

        <xs:element name="entry" type="atom:entryType"/>
        <xs:complexType name="entryType">
              <xs:annotation>
                 <xs:documentation>
                    The Atom entry construct is defined in accordance with the XML defined in Just-in doc.
                 </xs:documentation>
              </xs:annotation>

              <xs:choice maxOccurs="unbounded">
                 <xs:element name="id" type="atom:idType"/>
                 <xs:element name="link" type="atom:linkType" minOccurs="0" maxOccurs="unbounded"/>
                 <xs:element name="link" type="atom:secondLinkType" minOccurs="1" maxOccurs="unbounded"/>
                 <xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
              </xs:choice>

              <xs:attributeGroup ref="atom:commonAttributes"/>
           </xs:complexType>

    <xs:simpleType name="KnownRelCodeType">
          <xs:restriction base="xs:string">
             <xs:enumeration value="alternate">
                <xs:annotation>
                   <xs:documentation>
                   The value "alternate" signifies that the IRI in the value of the href attribute identifies an alternate version of the resource described by the containing element.
                   </xs:documentation>
                </xs:annotation>
             </xs:enumeration>

          </xs:restriction>
       </xs:simpleType>


       <xs:simpleType name="SecondKnownRelCodeType">
          <xs:restriction base="xs:string">
             <xs:enumeration value="amphtml">
                <xs:annotation>
                   <xs:documentation>
                      The value "amphtml" signifies that....
                   </xs:documentation>
                </xs:annotation>
             </xs:enumeration>

          </xs:restriction>
       </xs:simpleType>


       <xs:simpleType name="RelCodeType">
          <xs:union memberTypes="atom:KnownRelCodeType xs:string"/>
       </xs:simpleType>


       <xs:simpleType name="SecondRelCodeType">
          <xs:union memberTypes="atom:SecondKnownRelCodeType xs:string"/>
       </xs:simpleType>


    <xs:complexType name="linkType" mixed="true">
          <xs:annotation>
             <xs:documentation>
             The Atom link construct is defined in section 3.4 of the format spec.
             </xs:documentation>
          </xs:annotation>

          <xs:attribute name="href" type="xs:anyURI" use="required"/>
          <xs:attribute name="rel" type="atom:RelCodeType" use="required"/>
          <xs:attribute name="type" type="xs:string" use="required"/>
          <xs:attributeGroup ref="atom:commonAttributes"/>
       </xs:complexType>


       <xs:complexType name="secondLinkType" mixed="true">
      <xs:annotation>
         <xs:documentation>
            complex type for link rel="amphtml"
         </xs:documentation>
      </xs:annotation>
      <xs:attribute name="rel" type="atom:SecondRelCodeType" use="required"/>
      <xs:attribute name="href" type="xs:anyURI" use="required"/>
      <xs:attributeGroup ref="atom:commonAttributes"/>
      <xs:element name="inline:inline" type="inline:inlineType"/>
      <xs:attributeGroup ref="atom:commonAttributes"/>
   </xs:complexType>
kjhughes
  • 106,133
  • 27
  • 181
  • 240

1 Answers1

0

No, XML sibling elements cannot be named the same but typed differently in XSD.

Give the elements different names, or use the same type for both, possibly using optionality to accommodate the union of the possibilities afforded by the individual types.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Hi, I changing XML is not possible for me here. Also, I can not use the same type here because I have to define different attributes for each element that have same name. Could you please give an example of the suggestion you provided? I am unable to understand when you said "possibly using optionality to accommodate the union of the possibilities afforded by the individual types." – bhavishya tyagi May 24 '17 at 20:49