xmlrelaxngrelaxng-compact

What if a sub element is used by two other independent elements when using RELAX NG Compact?


I am using RELAX NG Compact and have run across a case where a sub-element is being used by two independent parent elements. How can I resolve this?

Use case 1

<parent1>
  <field usecase_123="test" />
</parent1>

Use case 2

<parent2>
  <field usecase_AAA="test" />
</parent2>

Herein lies the conflict:

parent1 = element parent1 { element field { attribute usecase_123 {text} } }

parent2 = element parent2 { element field { attribute usecase_AAA {text} } }

Solution

  • RelaxNG allows an element to have different attributes based on its parent element.

    So you can have a RelaxNG grammar like this:

    start = root
    root = element root { parent1* & parent2* }
    parent1 = element parent1 { element field { attribute usecase_123 {text} } }
    parent2 = element parent2 { element field { attribute usecase_AAA {text} } }
    

    And, valid against that grammar, the following document instance:

    <root>
      <parent1>
        <field usecase_123="test" />
      </parent1>
      <parent2>
        <field usecase_AAA="test" />
      </parent2>
    </root>
    

    …while, invalid against that grammar, the following document instance:

    <root>
      <parent1>
        <field usecase_AAA="test" />
      </parent1>
    </root>