恐怕真的没有一个非常不笨重的方法来做这件事。
我会采取两种方法中的一种。构建模式或在可以使用两种语言进行处理的环境中使用Schematron注释和处理:
例如,建立B元素组:
<define name="B2">
<ref name="B"/>
<optional><ref name="B"/></optional>
</define>
<define name="B4">
<ref name="B2"/>
<ref name="B2"/>
</define>
让你慢慢建立一组(在本例中),一个或两个元素,然后2-4个元素,依此类推。组合不同的组将允许您指定适当的计数(最终)。
或者,您可以使用Schematron注释:
<?xml version="1.0" encoding="utf-8"?>
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
xmlns:s="http://www.ascc.net/xml/schematron">
<element name="A">
<s:rule context="A">
<!-- note - using XPath 2 here -->
<s:assert test='count(B) lt 6'>A elements may contain no more than 6 B elements</s:assert>
</s:rule>
<ref name="B"/>
<oneOrMore><ref name="B"/></oneOrMore>
</element>
<define name="B">
<element name="B">
<text/>
</element>
</define>
</grammar>