6

I have an XML file like this

<listOfA>
  <a type="1">
    <name></name>
    <surname></surname>
  </a>
  <a type="2">
    <name></name>
    <id></id>
  </a>
</listOfA>

I'd like to make an XSD, so that if the value of the attribute "type" is 1, the name and surname elements must be present, and when it's 2, name and id must be there. I tried to generate the XSD in XSD schema generator, but it made the surname and id element minOccurs=0. How could I make it work?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
DropDropped
  • 1,253
  • 1
  • 22
  • 50
  • 1
    This is the sort of case covered by conditional type assignment in XSD 1.1. Are you able to use XSD 1.1? You won't be able to enforce such a constraint using XSD 1.0 alone; would need to use optionality in XSD 1.0 and then tie things down outside of XSD. – kjhughes Jan 10 '15 at 16:57
  • @kjhughes XSD 1.1 is no problem – DropDropped Jan 10 '15 at 17:50

1 Answers1

9

You can do this using XSD 1.1's Conditional Type Assignment:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           elementFormDefault="qualified"
           vc:minVersion="1.1"> 
  <xs:element name="listOfA">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="a" maxOccurs="unbounded">
          <xs:alternative test="@type = 1" type="a1Type"/>        
          <xs:alternative test="@type = 2" type="a2Type"/>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="a1Type">
    <xs:sequence>
      <xs:element name="name"/>
      <xs:element name="surname"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="a2Type">
    <xs:sequence>
      <xs:element name="name"/>
      <xs:element name="id"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • The MS VS 2015 do make error "The 'http://www.w3.org/2001/XMLSchema:alternative' element is not supported in this context" when just copying it (abover) to an empty page, – Jan Bergström Jul 24 '16 at 14:12
  • 1
    That's because .NET doesn't directly support XSD 1.1 (which this question indicates is acceptable). If you're using .NET and would like to use XSD 1.1, ask Microsoft to add support, or use Saxon. – kjhughes Jul 24 '16 at 16:14
  • Good explaination. What I understand this is the main weakness of XSD 1.1, it is not new and not much implemented? And as such problematic to use in Commercial standard software (as I do), in difference to in-house works? As far as I know " – Jan Bergström Jul 26 '16 at 13:45
  • If XML can be redesigned, need for XSD 1.1 and CTA can be avoided. (Think `` rather than ``.) However, please ask a new question rather than extend discussions in multiple directions in comments. Thanks. – kjhughes Jul 26 '16 at 14:19
  • Fine, this comment thread is ended and quite OK. I will not redesign XML but the response from you helps toi understand the condistions of present dayXML. – Jan Bergström Jul 27 '16 at 15:26
  • I would like to use this functionality in [tag:coldfusion]. How do I find out if my version of ColdFusion supports XSD 1.1? – Sander Aug 21 '17 at 14:10
  • 2
    @Sander: If you cannot determine whether ColdFusion supports XSD 1.1 from its documentation, you could always just try it. If it doesn't support XSD 1.1, then you should receive an error about the content of the element wrapping the `xs:alternative` not matching the expected XSD 1.0 content model (`(annotation?, (simpleType | complexType)?, (unique | key | keyref)*)`). – kjhughes Aug 21 '17 at 14:20