开发者

How to implement constraints / conditional operators in Relax NG?

Consider the following Relax NG schema written in Compact Syntax

key = element key { type, value }
type = element type { text }
开发者_StackOverflow社区value = element value { text }

Hereby any XML document like

 <key>
    <type>someType</type>
    <value>someValue</value>
 </key>

will be confirmed as well-formed. Now I want to put some constraints, e.g. some dependences between elements values. For example

if type:text = "digit"  then value:text = "[0-9]" else
if type:text = "letter" then value:text = "[a-z]"

This will filter out some invalid documents like

<key>
   <type>letter</type>
   <value>7</value> 
</key>
<!-- illegal - 7 is not a letter ! -->

The syntax of Relax NG does not provide an explicit mechanism to write conditional operators, so my question is how to emulate such behaviour and implement the dependence between values of some elements and attributes ?

Also I will be thankful if you show me a way to perform this using classic DTD.

Any help will be appreciated. Thanks in advance.


This schema:

start = key
key = element key { (type1, value1) | (type2, value2) }

type1 = element type { "letter" }
type2 = element type { "digit" } 

value1 = element value { xsd:string { pattern = "[a-z]" }}
value2 = element value { xsd:string { pattern = "[0-9]" }}

can be used to validate this document:

<key>
  <type>letter</type>
  <value>7</value>
</key>

Jing reports:

so.xml:3:20: error: character content of element "value" invalid; must be a string matching the regular expression "[a-z]"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜