[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Conditional statements in DTD?

  • From: "G. Ken Holman" <gkholman@C...>
  • To: <xml-dev@l...>
  • Date: Wed, 11 Mar 2009 14:47:48 -0400

Re:  Conditional statements in DTD?
At 2009-03-11 18:15 +0000, Graeme Kidd wrote:
>I am certain you cant use conditional statements within the DTD file 
>so is it possible to make an attribute valid only if a parent 
>element had an attribute set to something e.g.
>
>The 'StepTwo' element is valid as the 'allow' attribute in 'StepOne' 
>is set to 'yes'
>
>< root>
>    < StepOne allow="yes">
>       < StepTwo playing="hard"
>    < / StepOne
>< / root>
>
>The 'StepTwo' element is invalid as the 'allow' attribute in 
>'StepOne' is set to 'no'
>
>< root>
>    < StepOne allow="no">
>       < StepTwo playing="hard">
>    < / StepOne
>< / root>
>
>The 'StepTwo' element is valid because the 'allow' attribute is set 
>to 'no' and there is no attribute for Step Two.
>
>
>< root>
>    < StepOne allow="no">
>       < StepTwo>
>    < / StepOne
>< / root>
>
>Surely things like this could be rather common?

Indeed they are!  But not using the semantics available in DTD 
validation.  So you won't find what you need there.

Check out ISO/IEC 19757-3 Schematron:

   http://www.Schematron.com

... because you need an assertion validation language.  You want to 
assert that an attribute in StepTwo is allowed only when the "allow" 
attribute in StepOne is set to "yes", but, say, that StepTwo is 
allowed not to have an attribute at any time.

If you can move away from DTD-speak, then also check out ISO/IEC 
19757-2 RELAX-NG:

   http://www.relaxng.org

... where the validation semantics do allow you to specify what you want.

I hope the examples below of each of Schematron and RELAX-NG help.

. . . . . . . Ken


T:\ftemp>type graeme1.xml
<root>
    <StepOne allow="yes">
       <StepTwo playing="hard"/>
    </StepOne>
</root>

T:\ftemp>type graeme2.xml
<root>
    <StepOne allow="no">
       <StepTwo playing="hard"/>
    </StepOne>
</root>

T:\ftemp>type graeme3.xml
<root>
    <StepOne allow="no">
       <StepTwo/>
    </StepOne>
</root>

T:\ftemp>type graeme4.xml
<root>
    <StepOne>
       <StepTwo playing="hard"/>
    </StepOne>
</root>

T:\ftemp>type graeme5.xml
<root>
    <StepOne>
       <StepTwo/>
    </StepOne>
</root>

T:\ftemp>type graeme.sch
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
   <title>Graeme's test</title>

   <pattern>
     <rule context="StepTwo[@playing]">
       <assert test="parent::StepOne/@allow='yes'"
           >StepTwo can have an attribute only when StepOne allows it.</assert>
     </rule>
   </pattern>
</schema>

T:\ftemp>call xslt graeme.sch iso_schematron_terminator.xsl graeme.xsl

T:\ftemp>call xslt graeme1.xml graeme.xsl
<?xml version="1.0" encoding="utf-8"?>
T:\ftemp>echo errorlevel = 0
errorlevel = 0

T:\ftemp>call xslt graeme2.xml graeme.xsl
StepTwo can have an attribute only when StepOne allows it.: 
/root/StepOne/StepTwo

Processing terminated by xsl:message at line 164

T:\ftemp>echo errorlevel = 1
errorlevel = 1

T:\ftemp>call xslt graeme3.xml graeme.xsl
<?xml version="1.0" encoding="utf-8"?>
T:\ftemp>echo errorlevel = 0
errorlevel = 0

T:\ftemp>call xslt graeme4.xml graeme.xsl
StepTwo can have an attribute only when StepOne allows it.: 
/root/StepOne/StepTwo

Processing terminated by xsl:message at line 164

T:\ftemp>echo errorlevel = 1
errorlevel = 1

T:\ftemp>call xslt graeme5.xml graeme.xsl
<?xml version="1.0" encoding="utf-8"?>
T:\ftemp>echo errorlevel = 0
errorlevel = 0

T:\ftemp>type graeme.rnc
start = element root { s1 }

s1 =
  (
    element StepOne # when the child is allowed to have an attribute
     {
        attribute allow { "yes" },
        element StepTwo
        {
           attribute playing { text }?
        }
     }
  |
    element StepOne # when the child is not allowed to have an attribute
     {
        attribute allow { "no" }?,
        element StepTwo { empty }
     }
  )

T:\ftemp>call rnc graeme.rnc graeme1.xml

T:\ftemp>call rnc graeme.rnc graeme2.xml
T:\ftemp\graeme2.xml:3:32: error: attribute "playing" not allowed at 
this point; ignored

T:\ftemp>call rnc graeme.rnc graeme3.xml

T:\ftemp>call rnc graeme.rnc graeme4.xml
T:\ftemp\graeme4.xml:3:32: error: attribute "playing" not allowed at 
this point; ignored

T:\ftemp>call rnc graeme.rnc graeme5.xml

T:\ftemp>rem




--
XQuery/XSLT training in Prague, CZ 2009-03 http://www.xmlprague.cz
XQuery/XSLT/XSL-FO training in Los Angeles (New dates!) 2009-06-08
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman@C...
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/x/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/x/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal



[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.