[relaxng-user] Re: Recursive structure with restrictions

Tor Helland tor at helland.org
Wed Jun 23 23:22:46 ICT 2004


> Playing with grammars, I found a solution demoed in the three documents at
> the end of this email. I like the control I get over the structure, and
not
> repeating it.
>
> Is there a better way to achieve this, preferrably with one schema file?
Is
> there a simpler way, something obvious I have overlooked?

Summer is here, and this list takes a dive, so I thought I had to answer
myself.

Using a general entity from the old xml and dtd world, I could replace the
include element.

-tor

test.rng:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE grammar [
  <!ENTITY abc-elements '
    <define name="a-element">
      <grammar>
        <start>
          <element name="a">
            <empty/>
            <optional>
              <parentRef name="container-choice"/>
            </optional>
          </element>
        </start>
      </grammar>
    </define>
    <define name="b-element">
      <grammar>
        <start>
          <element name="b">
            <empty/>
            <optional>
              <parentRef name="container-choice"/>
            </optional>
          </element>
        </start>
      </grammar>
    </define>
    <define name="c-element">
      <grammar>
        <start>
          <element name="c">
            <empty/>
            <optional>
              <parentRef name="container-choice"/>
            </optional>
          </element>
        </start>
      </grammar>
    </define>
  '>
]>

<grammar ns="test" xmlns="http://relaxng.org/ns/structure/1.0">
  <start>
    <element name="test">
      <empty/>
      <ref name="container-abc-element"/>
      <ref name="container-ab-element"/>
      <ref name="container-c-element"/>
    </element>
  </start>

  <define name="container-abc-element">
    <grammar>
      &abc-elements;

      <start>
        <element name="container-abc">
          <ref name="container-choice"/>
        </element>
      </start>
      <define name="container-choice">
        <choice>
          <ref name="a-element"/>
          <ref name="b-element"/>
          <ref name="c-element"/>
        </choice>
      </define>
    </grammar>
  </define>
  <define name="container-ab-element">
    <grammar>
      &abc-elements;

      <start>
        <element name="container-ab">
          <ref name="container-choice"/>
        </element>
      </start>
      <define name="container-choice">
        <choice>
          <ref name="a-element"/>
          <ref name="b-element"/>
        </choice>
      </define>
    </grammar>
  </define>
  <define name="container-c-element">
    <grammar>
      &abc-elements;

      <start>
        <element name="container-c">
          <ref name="container-choice"/>
        </element>
      </start>
      <define name="container-choice">
        <choice>
          <ref name="c-element"/>
        </choice>
      </define>
    </grammar>
  </define>

</grammar>

End of email



More information about the relaxng-user mailing list