views:

110

answers:

2

Hello all!

I have the following case: All boats have a boat type like shark , yatch and so on. I need to register which type of boat name and also how many feet the boat is, but this is where the problem arises. If the user types in a shark I need to validate that its between 15-30 feet, if he type in a yatch it needs to be between 30-60 for instance.

Any help on this?

<boat>
    <type>shark</type>
    <foot>18</foot> //validates
</boat>
<boat>
    <type>shark</type>
    <foot>14</foot> //fails
</boat>
<boat>
    <type>AnyOtherBoat</type>
    <foot>14</foot>//validates since its another type of boat than shark and yatch
</boat>

Help appriciated! Thx

+3  A: 

Schematron ("a language for making assertions about patterns found in XML documents") might be able to do what you need. It allows specifying additional rules which cannot be expressed within a regular XML schema definition (XSD, RelaxNG).

Here are some articles to get you started:

  1. Schematron on Wikipedia
  2. Schematron: XML Structure Validation Language Using Patterns in Trees
  3. Improving XML Document Validation with Schematron
0xA3
+1  A: 

To answer your question: No, you can't do that in XML Schema.

  • Firstly, you can't use values to select which constraints to apply (but you could for elements like <shark>)
  • Secondly, you can't do arithmetic tests (but you can use regex to specify the permissible strings... so you might be able to hack it.)
13ren
Thank you! I couldnt find the info to do this anywhere either. Yea Im aware of I could allow elements like <shark> and put constraints on that element, but thats not what was needed here. Im really curious what my teacher will say to this matter, and what he expects from this question.
ChrisAD
Maybe he's thinking of the new version of XML Schema that's coming out? (I don't think it's out yet; and I don't think it can do this - but might be worth checking on. If you do, and it does what you want, please let me know.)
13ren