How can you specify co-dependent attributes in XSD?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tskelly
    New Member
    • Mar 2010
    • 2

    How can you specify co-dependent attributes in XSD?

    Hi,

    Is there any way of specifying a pair of attributes that are both optional but if one is present the other must also be?

    Thanks in advance.
  • rski
    Recognized Expert Contributor
    • Dec 2006
    • 700

    #2
    You can try to achieve this using abstract types and xsi:type attribute

    xsd
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/GolfCountryClub" xmlns:tns="http://www.example.org/GolfCountryClub">
    
    	<element name="osoby">
    		<complexType>
    			<sequence>
    				<element name="osoba" type="tns:tagType"></element>						
    			</sequence>
    		</complexType>
    	</element>	
      
      
      
    	<complexType name="tagType" abstract="true" />
    
    	<complexType name="tagWithAttr">
       		<complexContent mixed="true">
          		<extension base="tns:tagType">
             		<attribute name="attr1" type="string" use="required"/>
             		<attribute name="attr2" type="string" use="required"/>
          		</extension>
       		</complexContent>
    	</complexType>
    
    	<complexType name="tagWithoutAttr">
       		<complexContent mixed="true">
          		<extension base="tns:tagType">
          		</extension>
       		</complexContent>
    	</complexType>
    </schema>
    valid xmls
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <tns:osoby xmlns:tns="http://www.example.org/GolfCountryClub" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/GolfCountryClub GolfCountryClub.xsd ">
      <osoba attr1="18" attr2="19" xsi:type="tns:tagWithAttr"/>
    </tns:osoby>
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <tns:osoby xmlns:tns="http://www.example.org/GolfCountryClub" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/GolfCountryClub GolfCountryClub.xsd ">
      <osoba xsi:type="tns:tagWithoutAttr"/>
    </tns:osoby>
    nonvalid xmls
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <tns:osoby xmlns:tns="http://www.example.org/GolfCountryClub" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/GolfCountryClub GolfCountryClub.xsd ">
      <osoba attr1="18"  xsi:type="tns:tagWithoutAttr"/>
    </tns:osoby>

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <tns:osoby xmlns:tns="http://www.example.org/GolfCountryClub" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/GolfCountryClub GolfCountryClub.xsd ">
      <osoba attr1="18"  xsi:type="tns:tagWithAttr"/>
    </tns:osoby>
    Is that ok for you?

    Comment

    • tskelly
      New Member
      • Mar 2010
      • 2

      #3
      Hi rski,

      that's a good solution ... wish I had thought of it!!

      However, we can't use it in this instance. We have written a system to process customer transmitted files conforming to a specific xsd that we provide. Our system is deployed and live and cannot be changed. This solution would require a code change.

      Thanks though.

      Comment

      • rski
        Recognized Expert Contributor
        • Dec 2006
        • 700

        #4
        Is it possible for you to add some Relax-NG formulas into the xsd file?

        Comment

        Working...