1

thats a part of my xml file:

<MY_ATTRIBUTES>
  <ATTRIBUTES>
      <ID>first_id</ID>
      <TYPE>first_type</TYPE>
      <FILE1>my_file1</FILE1>
  </ATTRIBUTES>
  <ATTRIBUTES>
      <ID>second_id</ID>
      <TYPE>second_type</TYPE>
      <FILE2>my_file2</FILE2>
      <FILE3>my_file3</FILE3>
  </ATTRIBUTES>
</MY_ATTRIBUTES>

I need to write the corresponding xsd file. my problem is that number and name of the elements depends on the text of the TYPE element. I tried to do this via xs:alternative but I wasn't successful with that:

<xs:element name="TYPE" type="xs:string">
    <xs:alternative test="text() eq first_type'" type="first_type"/>
    <xs:alternative test="text() eq second_type'" type="second_type"/>
</xs:element>


<xs:complexType name="first_type">
    <xs:sequence>
        <xs:element name="TYPE" type="xs:string" fixed="first_type"/>
        <xs:element name="FILE1" type="xs:string"/>
    </xs:sequence>
</xs:complexType>

any idea how this is possible?

Joe
  • 23
  • 2

2 Answers2

0

xs:alternative is part of Conditional Type Assignment which requires:

  • XSD 1.1
  • That xs:alternative/@test test an attribute, not an element.

If you're able to use XSD 1.1 and able to redesign your XML so that the type information is contained in an attribute, you may then follow the example given in How to make type depend on attribute value using Conditional Type Assignment.

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • 1
    To amplify this, the reason that conditional type assignment only allows the type to depend on attribute values is that XSD by design never requires backtracking: the required structure of the content must be known before you start reading the content. – Michael Kay Aug 03 '17 at 17:46
0

Of course I am able tp resedign my xml like this

  <MY_ATTRIBUTES>
      <ATTRIBUTES ID="first_id" type="first_type">
          <FILE1>my_file1</FILE1>
      </ATTRIBUTES>
      <ATTRIBUTES ID="second_id" type="second_type">
          <FILE2>my_file2</FILE2>
          <FILE3>my_file3</FILE3>
      </ATTRIBUTES>
   </MY_ATTRIBUTES>

But it looks like its not possible to use XSD1.1 because it is not availble with python. Is there any other solution to solve this with XSD1.0?

Joe
  • 23
  • 2