2 Replies - 3205 Views - Last Post: 31 July 2012 - 06:58 PM Rate Topic: -----

#1 UziTech  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 64
  • Joined: 26-October 10

XML Schema required attribute when multiple elements

Posted 29 July 2012 - 07:27 PM

is there a way to write a schema so an attribute is required only if there is multiple elements?

Valid XML examples:

<root>
  <elem name="x"/>
</root>

<root>
  <elem name="x" option="1" />
  <elem name="x" option="2" />
</root>



Invalid XML examples:

<root>
  <elem name="x" option="1" />
</root>

<root>
  <elem name="x" />
  <elem name="x" />
</root>



Is This A Good Question/Topic? 0
  • +

Replies To: XML Schema required attribute when multiple elements

#2 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 771
  • Joined: 27-June 09

Re: XML Schema required attribute when multiple elements

Posted 31 July 2012 - 09:45 AM

I think you can accomplish this with a xs:choice
Was This Post Helpful? 0
  • +
  • -

#3 UziTech  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 64
  • Joined: 26-October 10

Re: XML Schema required attribute when multiple elements

Posted 31 July 2012 - 06:58 PM

View Postmojo666, on 31 July 2012 - 11:45 AM, said:

I think you can accomplish this with a xs:choice


I tried this:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="xml" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:choice>
        <xs:element name="elem" minOccurs="0" maxOccurs="1">
          <xs:complexType>
            <xs:attribute name="name" type="xs:string" use="required"/>
          </xs:complexType>
        </xs:element>
        <xs:element name="elem" minOccurs="2" maxOccurs="unbounded">
          <xs:complexType>
            <xs:attribute name="option" type="xs:string" use="required"/>
            <xs:attribute name="name" type="xs:string" use="required"/>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>



but it says the element names to choose from are ambiguous because the choice is between elem and elem it doesn't look at any other attributes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1