Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Same element, different required content?

Status
Not open for further replies.

goBoating

Programmer
Feb 8, 2000
1,606
US
I've tried the search function and get no usable results. So, sorry if this is a noobie question.... it may very well be. I'm a noob to playing with this stuff.

I am trying to write a schema that will enforce the content (value) of two elements that have the same name.

For instance, if the XML looked like:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<root_element 
xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance"[/URL] 
xsi:noNamespaceSchemaLocation="example.xsd">
	<registrant>
		<color>blue</color>
		<color>blue</color>
	</registrant>
</root_element>

I'd use a schema that looked like:
Code:
<?xml version="1.0"?>
<xsd:schema 
xmlns="[URL unfurl="true"]http://www.csc.noaa.gov/ioos/schema"[/URL] 
xmlns:xsd="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema"[/URL] 
elementFormDefault="qualified" 
attributeFormDefault="qualified">
<xsd:element name="root_element">
  <xsd:complexType>
    <xsd:choice maxOccurs="unbounded">
      <xsd:element name="registrant">
        <xsd:complexType>
          <xsd:sequence>
          <xsd:element name="color">
            <xsd:simpleType>
              <xsd:restriction base="xsd:string">
                <xsd:enumeration value="blue"/>
              </xsd:restriction>
            </xsd:simpleType>
          </xsd:element>
          <xsd:element name="color"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:choice>
  </xsd:complexType>
</xsd:element>
</xsd:schema>

However, the XML needs to have two 'color' elements one containing 'blue' and the second containing 'red'. This is a very small part of a much nastier schema.

I'd like to be able to use two different types for the 'color' element, like:
Code:
<?xml version="1.0"?>
<xsd:schema 
xmlns="[URL unfurl="true"]http://www.csc.noaa.gov/ioos/schema"[/URL] 
xmlns:xsd="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema"[/URL] 
elementFormDefault="qualified" 
attributeFormDefault="qualified">
<xsd:element name="root_element">
  <xsd:complexType>
    <xsd:choice maxOccurs="unbounded">
      <xsd:element name="registrant">
        <xsd:complexType>
          <xsd:sequence>
          
          <xsd:element name="color">
            <xsd:simpleType>
              <xsd:restriction base="xsd:string">
                <xsd:enumeration value="blue"/>
              </xsd:restriction>
            </xsd:simpleType>
          </xsd:element>
          
          <xsd:element name="color">
            <xsd:simpleType>
              <xsd:restriction base="xsd:string">
                <xsd:enumeration value="red"/>
              </xsd:restriction>
            </xsd:simpleType>
          </xsd:element>
          
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:choice>
  </xsd:complexType>
</xsd:element>
</xsd:schema>


How do a control the content of the sequential 'color' elements so that the first contains 'blue' and the second contains 'red'? Apparently, it is illegal to use two different 'types' for the same element name. Help?

Thanks a ton.

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
>it is illegal to use two different 'types' for the same element name.
To be precise and that is important: "..., withinin the same scope." That kind of constraints is not the concern of the existing form of the w3c-xml-schema-language.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top