RelaxNG enumerated element names

2019-08-23 02:00发布

问题:

If I have element names that must look like this:

<myElem>
  <subElem_n/>
  <subElem_n+1/>
  <subElem_n+2/>
</myElem>

Where 'n' = 0;

How would I enforce this is RelaxNG?

The tricky part is the dynamically generated element names.

回答1:

Concerning your XML sample file :

The XML you provide is not well formed. The + is not allowed in XML names. See here, have a look at PrefixedName and UnprefixedName to have precise syntax.

Concerning your validation case :

As far as I know, you can't add dynamically generated validation rules on element names with Relax NG.

The only way to check this kind of structure is to use either ISO Schematron or the upcoming XML Schema 1.1 recommandation with the xs:assert element.

Those two solutions use XPath and you can check the names with the local-name() function.

One more thing

Be aware that it's not always considered a good practice to have such structure, you can also consider this kind :

<myElem>
    <subElem rank="1"/>
    <subElem rank="2"/>
    <subElem rank="3"/>
    ...
</myElem>


标签: xml relaxng