Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a sequence of XML elements be conditional on a property?

First question (be kind!) Explanation: if a property's true, I need the type to have elements. So if an attribute is true, the XML output might be:

<Approval Approved="true">
   <By>RT</By>
   <Date>27/07/2011</Date>
</Approval>

And if it isn't approved, the XML output might be

<Approval Approved="false" />

Is it possible to specify something like this in an XSD?

like image 651
struct Avatar asked Jul 27 '11 12:07

struct


People also ask

Does order of XML elements matter?

In the most general sense, XML element order does not matter, unless otherwise specified by the appropriate schema.

Which rule is followed while defining elements in XML?

XML Naming RulesElement names must start with a letter or underscore. Element names cannot start with the letters xml (or XML, or Xml, etc) Element names can contain letters, digits, hyphens, underscores, and periods. Element names cannot contain spaces.

What are the elements naming and nesting conventions in XML?

All XML documents must contain a single tag pair to define a root element. All other elements must be within this root element. All elements can have sub elements (child elements). Sub elements must be correctly nested within their parent element.


1 Answers

Turns out, you can do it (sort of) but the method totally sucks.

Had to make two complex types (one with the Approved tag and one without), change the root element and allow switching between the two types like this:

<xs:element name="ArchivedFormulation">
 <xs:complexType>
  <xs:choice>
    <xs:element name="ApprovedFormulation" type="ApprovedFormulation" />
    <xs:element name="NonApprovedFormulation" type="NonApprovedFormulation" />
  </xs:choice>
</xs:complexType>

Could add the complex types using XSD inheritance.

<xs:complexType name="ApprovedFormulation">
<xs:complexContent>
  <xs:extension base="NonApprovedFormulation">
    <xs:sequence>
      <xs:element name="Approved" minOccurs="1" maxOccurs="1">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="ApprovedBy" type="xs:string" />
            <xs:element name="ApprovedOn" type="xs:date" />
          </xs:sequence>
          <xs:attribute name="IsApproved" type="xs:boolean" />
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:extension>
</xs:complexContent>

That gave me what I wanted.

like image 118
struct Avatar answered Oct 17 '22 03:10

struct