[Home] [By Thread] [By Date] [Recent Entries]

  • To: xml-dev@l...
  • Subject: Re: dtd attributes
  • From: "G. Ken Holman" <gkholman@C...>
  • Date: Mon, 26 Jul 2004 07:32:22 -0400
  • In-reply-to: <BAY17-F183qrB0Xn2Pi000829a2@h...>

At 2004-07-26 10:46 +0100, james walker wrote:
>Does anybody know how to force an attribute to be used when another 
>attribute is used?

This is called a co-occurrence constraint.

>I have a set of tags below:
><portlet imageurl="http://....." alt="this is an image" >
></portlet>
>sometimes the portlet may have an image and sometimes it may not. How do i 
>construct the dtd attributes to force the person writing the xml to always 
>have an alt attribute for every imageurl attribute.

DTD syntax is not expressive enough to express such a co-occurrence 
constraint.  Neither is W3C Schema.

You can express this in RELAX-NG.  An example is below.  Note how the 
parentheses makes the pair of attributes an item that, as a pair, is 
optional.  This means that neither may be allowed, or both may be allowed, 
but not only just one.

I hope this helps.

T:\ftemp>type walker.rng
start = element portlet
   {
    (
      attribute imageurl { text },
      attribute alt { text }
    )?
   }

T:\ftemp>type walker1.xml
<portlet imageurl="http://....." alt="this is an image" >
</portlet>
T:\ftemp>jing -c walker.rng walker1.xml

T:\ftemp>type walker2.xml
<portlet>
</portlet>
T:\ftemp>jing -c walker.rng walker2.xml

T:\ftemp>type walker3.xml
<portlet imageurl="http://.....">
</portlet>
T:\ftemp>jing -c walker.rng walker3.xml
T:\ftemp\walker3.xml:1: error: required attributes missing

T:\ftemp>

--
World-wide on-site corporate, govt. & user group XML/XSL training.
G. Ken Holman                 mailto:gkholman@C...
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/x/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Breast Cancer Awareness  http://www.CraneSoftwrights.com/x/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member