寻求可处理同层级重复元素名的XML验证语言
嘿,这个问题我太懂了!XML Schema(XSD)确实对同一层级的元素名唯一性有严格限制,尤其是当你需要让<item>元素同时支持多种结构(比如single类型、group类型,还有带ref引用的类型)时,XSD的局限性就直接暴露了。不过别慌,有几个成熟的替代方案能完美适配你的场景:
1. Relax NG
Relax NG是我首推的方案,它的设计理念就是灵活适配各种XML结构,完全没有XSD那种对同名元素的层级限制。你可以轻松定义同一层级下的<item>元素支持不同的属性和子元素组合。
比如用Relax NG Compact Syntax写的验证规则片段:
start = element items { element item { attribute type { "single" }, element name { text } } | element item { attribute type { "group" }, element name { text }, element groups { ... } } | element item { attribute ref { text } } }*
这个规则直接允许<items>下出现任意多个<item>,每个<item>可以是三种结构中的任意一种,完美匹配你的XML结构。
2. Schematron
Schematron是基于规则的验证语言,它不关心元素的结构约束,只关注你定义的业务规则。你可以用它来检查不同属性的<item>是否符合对应的结构要求,而且它可以和XSD配合使用,也能单独作为验证工具。
举个简单的Schematron规则示例:
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron"> <sch:pattern name="Item structure validation"> <!-- 验证type="single"的item --> <sch:rule context="item[@type='single']"> <sch:assert test="name">Type为single的item必须包含name元素</sch:assert> <sch:assert test="not(groups)">Type为single的item不能包含groups元素</sch:assert> </sch:rule> <!-- 验证type="group"的item --> <sch:rule context="item[@type='group']"> <sch:assert test="name and groups">Type为group的item必须同时包含name和groups元素</sch:assert> </sch:rule> <!-- 验证带ref属性的item --> <sch:rule context="item[@ref]"> <sch:assert test="not(type)">带ref属性的item不能同时有type属性</sch:assert> <sch:assert test="not(*)">带ref属性的item不能包含子元素</sch:assert> </sch:rule> </sch:pattern> </sch:schema>
这种方式完全不限制同一层级的<item>数量,只按规则验证每个元素的合规性。
3. XSD 1.1(条件类型赋值)
如果你还是想尽量用XSD生态,XSD 1.1引入了条件类型赋值(Conditional Type Assignment),可以根据元素的属性值为同名元素指定不同的类型。不过要注意,不是所有XML处理器都支持XSD 1.1,比如旧版本的解析器可能需要升级。
示例片段:
<xs:element name="items"> <xs:complexType> <xs:sequence> <xs:element name="item" maxOccurs="unbounded"> <xs:alternative test="@type='single'" type="SingleItemType"/> <xs:alternative test="@type='group'" type="GroupItemType"/> <xs:alternative test="@ref" type="RefItemType"/> </xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="SingleItemType"> <xs:sequence> <xs:element name="name" type="xs:string"/> </xs:sequence> <xs:attribute name="type" fixed="single"/> </xs:complexType> <!-- 这里省略GroupItemType和RefItemType的定义 -->
这个方案能让XSD支持你的XML结构,但兼容性是需要考虑的点。
总结一下:如果追求最大灵活性和兼容性,选Relax NG;如果需要复杂的业务规则验证,Schematron是绝佳选择;如果必须用XSD,那就尝试XSD 1.1的条件类型赋值。
内容的提问来源于stack exchange,提问作者Robert Whitebit

