Relax NG and Uniqueness of Elements / Attributes

Can a Relax NG element / attribute be made unique?

For example, the attribute:

<rng:attribute name="test"> <rng:ref name="options"/> </rng:attribute> 

links to:

 <rng:define name="options"> <rng:choice> <rng:value>t1</rng:value> <rng:value>t2</rng:value> <rng:value>t3</rng:value> </rng:choice> </rng:define> 

Now I need to check the xml, which should not use one of the β€œoptions” twice. In other words: in the xml should not appear "t1" twice ...

I read something about the schematron. But is there any other possibility?

+4
source share
2 answers

No, uniqueness restrictions and referential integrity restrictions are not supported by Relax NG. As James Clarke says in the Relax NG design paper :

RELAX NG TC spent a lot of time, considering what support for RELAX NG should ensure compliance with restrictions (uniqueness and cross-reference). In conclusion, it was concluded that personality limitations are better divided into individual specifications. Accordingly, RELAX NG alone does not support identification restrictions.

For XML compatibility, DTD RelaxNG supports ID / IDREF constraint checking. But this is one of the areas where it is easiest to encounter inconsistencies between processors and confuse new users.

Your options include

  • checking the uniqueness of values ​​at the application level
  • using a different schema language (Schematron, DTDs, XSD) to formulate and enforce this restriction (of which Schematron is probably the easiest to use in practice as a way to complement RelaxNG at specific points, but leave most of RelaxNG's work)
  • rejiggering XML, so that the difference between t1, t2 and t3 is expressed in the names of the elements, and the content model can ensure their uniqueness; this is not necessarily possible.
+2
source

Your schematic fragments are fine.

Scheme:

  <? xml version = "1.0"?>
 <rng: element xmlns: rng = "http://relaxng.org/ns/structure/1.0" name = "doc">
   <rng: attribute name = "test">
     <rng: choice>
       <rng: value> t1 </ rng: value>
       <rng: value> t2 </ rng: value>
       <rng: value> t3 </ rng: value>
     </ rng: choice>
   </ rng: attribute>
 </ rng: element>

allows the attribute test to have the value "t1", "t2" or "t3". If it contains anything else, the RELAX NG validator will display a validation error message. For example, in this case jing reports

  "[Jing] value of attribute" test "is invalid; must be equal to" t1 "," t2 "or" t3 " 
.
0
source

Source: https://habr.com/ru/post/1346701/


All Articles