1

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} } }
Rod
  • 14,529
  • 31
  • 118
  • 230

1 Answers1

1

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>
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
  • I am definitely a noob here, so forgive me if I don't understand this still. Isn't that the same code I have posted but it's not valid for me. Other than start and root lines, does that have significance? – Rod Jun 30 '17 at 15:30
  • It’s valid. Is there some tool that’s telling you it’s invalid? If I put that grammar into a `test.rnc` file and I put the first document above into a `text.xml` file and I run `java -jar jing.jar -c test.rnc test.xml`, it validates successfully, as expected. And if I put the second document above into a `test2.xml` file and run jing on it against the same schema, it does not validate successfully, as expected. – sideshowbarker Jul 01 '17 at 01:32
  • Ok, thanks. Yeah, there's a custom program that's failing and not the validation. – Rod Jul 01 '17 at 01:50
  • Btw, I have another question if you get a chance, if not no worries...https://stackoverflow.com/q/44853309/139698 – Rod Jul 01 '17 at 01:53
  • Will take a look – sideshowbarker Jul 01 '17 at 01:54