Search code examples
xmlxml-validationrelaxng

RelaxNG validation for node with unknown structure


I need to build a RelaxNG validation schema, and I faced such problem:

I have a need to validate documents with with an <optional> tag inside, and this <optional> tag might enclose some other tags inside, and I don't know their schema, or they don't have unified one.

Simplified sample:

<?xml version="1.0" encoding="UTF-8"?>
<elements>
  <first>
    <name>John</name>
    <sname>Silver</sname>
  </first>
  <second>
    <phone>+123456789</phone>
    <mail>[email protected]</mail>
  </second>
  <optional>
     ...
  <optional>
</elements>

Here I know clearly what should be inside <first> and <second> tags, but <optional> may contain either text or complex node structure.

Is there something in RelaxNG to mark an element in the way, that it's internal structure will be ignored?

I am looking for something like <anyType /> here, but actually there is no such thing as anyType.

<element name="optional">
  <anyType />
  <zeroOrMore>
    <attribute>
      <anyName/>
    </attribute>
  </zeroOrMore>
</element>

There is also a tag <data> in RelaxNG, which specifies the element content type, but I can't find proper value for its type attribute, to solve my problem.


Solution

  • If you define your optional element like this:

    <element name="optional">
      <ref name="any"/>
    </element>
    

    And define any like this:

      <define name="any">
        <zeroOrMore>
          <choice>
            <attribute>
              <anyName/>
            </attribute>
            <text/>
            <element>
                <anyName/>
                <ref name="any"/>
            </element>
          </choice>
        </zeroOrMore>
      </define>
    

    You should be able to put whatever you want in optional. This will accept any element in any namespace whatsoever with any attributes that have any values, and whatever text between the elements.