mercredi 29 juillet 2015

RNG schema with unordered required, optional and arbitrary tags

This is a follow-up question to XSD schema with unordered required, optional and arbitrary tags

I am trying to make a RNG schema with the following constraints:

  1. There is no ordering
  2. Some elements must appear exactly once
  3. Some elements may appear zero or unbounded times
  4. Allow unrecognized elements (do not validate them)

Using the example in the previous question, I came up with the following RNG schema:

<grammar xmlns="http://ift.tt/1mso7nd" ns="" datatypeLibrary="http://ift.tt/1AQVtSM">

  <start>
    <element name="person">
      <interleave>                         <!-- no ordering -->
        <element name="name">              <!-- appears exactly once -->
          <data type="string"/>
        </element>
        <optional>                         <!-- appears 0 or 1 times -->
          <element name="age">
            <data type="integer"/>
          </element>
        </optional>
        <zeroOrMore>                       <!-- appears 0 or more times -->
          <element name="phone">
            <data type="integer"/>
          </element>
        </zeroOrMore>
        <ref name="anything"/>             <!-- allow any element -->
      </interleave>
    </element>
  </start>

  <define name="anything">
    <zeroOrMore>
      <choice>
        <element>
          <anyName>
            <except>
              <name>name</name>
              <name>age</name>
              <name>phone</name>
            </except>
          </anyName>
          <ref name="anything"/>
        </element>
        <attribute>
          <anyName/>
        </attribute>
        <text/>
      </choice>
    </zeroOrMore>
  </define>

</grammar>

It was easy enough to satisfy the first three constraints, but for the 4th (allowing unrecognized elements), I wasn't able to find a great solution. What I do is define "anything" to be one or more unrecognized elements, and then I reference it in the <person> element. The problem is that I have to explicitly specify exceptions in <anyName>: it should match any element that is not "name", "age" or "phone". If I leave out the <except>, I get the following error:

people.rng:5: element interleave: Relax-NG parser error : Element or text conflicts in interleave

The solution of having explicit exceptions looks clumsy and hackish. Is there a better way of allowing any element that isn't recognized? Is there anything similar to XSD's <any>? Thanks in advance for any help.




Aucun commentaire:

Enregistrer un commentaire