469,275 Members | 1,871 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

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.
Mar 11 '10 #1
3 4958
rski
700 Expert 512MB
You can try to achieve this using abstract types and xsi:type attribute

xsd
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/GolfCountryClub" xmlns:tns="http://www.example.org/GolfCountryClub">
  3.  
  4.     <element name="osoby">
  5.         <complexType>
  6.             <sequence>
  7.                 <element name="osoba" type="tns:tagType"></element>                        
  8.             </sequence>
  9.         </complexType>
  10.     </element>    
  11.  
  12.  
  13.  
  14.     <complexType name="tagType" abstract="true" />
  15.  
  16.     <complexType name="tagWithAttr">
  17.            <complexContent mixed="true">
  18.               <extension base="tns:tagType">
  19.                  <attribute name="attr1" type="string" use="required"/>
  20.                  <attribute name="attr2" type="string" use="required"/>
  21.               </extension>
  22.            </complexContent>
  23.     </complexType>
  24.  
  25.     <complexType name="tagWithoutAttr">
  26.            <complexContent mixed="true">
  27.               <extension base="tns:tagType">
  28.               </extension>
  29.            </complexContent>
  30.     </complexType>
  31. </schema>
  32.  
valid xmls
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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 ">
  3.   <osoba attr1="18" attr2="19" xsi:type="tns:tagWithAttr"/>
  4. </tns:osoby>
  5.  
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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 ">
  3.   <osoba xsi:type="tns:tagWithoutAttr"/>
  4. </tns:osoby>
  5.  
nonvalid xmls
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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 ">
  3.   <osoba attr1="18"  xsi:type="tns:tagWithoutAttr"/>
  4. </tns:osoby>
  5.  

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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 ">
  3.   <osoba attr1="18"  xsi:type="tns:tagWithAttr"/>
  4. </tns:osoby>
  5.  
Is that ok for you?
Mar 14 '10 #2
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.
Mar 15 '10 #3
rski
700 Expert 512MB
Is it possible for you to add some Relax-NG formulas into the xsd file?
Mar 15 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

12 posts views Thread by Jim Cochrane | last post: by
2 posts views Thread by Jeremy Collins | last post: by
2 posts views Thread by chrisn | last post: by
11 posts views Thread by Chris Ianson | last post: by
27 posts views Thread by Chris Tomlinson | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.