XSD以基于父属性验证具有不同序列元素的XML



我已经浏览了stackoverflow上的其他提及,但实际上看不到任何符合我想要的内容。

我已经使用JAXB生成了.xsd,但不幸的是,它不太好,所以当我意识到有一些东西似乎与我遵循的其他模式的逻辑不匹配时,我手动创建了一个.xsd。

与其用冗长的描述,我最好把它作为代码显示出来。我有很多xml文档,其中有重复的元素,这些元素根据其父(ItemGroup(元素id而不同。其中一些id是重复的(如图所示(,有些只出现一次(它们以0结尾或是DocDetails的一部分(。此外,特定"id"的元素总是相同的元素列表。

在该示例中,以0结尾的元素只出现一次,以1结尾的元素是重复的元素(尽管通常不超过2(,以2结尾的元素重复任何次数。这种后缀编号是人为的,以使其显而易见,但相同的固定ItemGroup id有1个或重复的元素。

<?xml version="1.0" encoding="UTF-8"?>
<DocTree xmlns="http://wolfedgx.com/doc-list/1.0/">
<DocDetails>
<Title>Book Title</Title>
<Author>Author Name</Author>
</DocDetails>
<ItemGroup id="10">
<GroupCategory>52</GroupCategory>
<Genre>Horror</Horror>
</ItemGroup>
<ItemGroup id="11">
<GroupHierarchy>52</GroupHierarchy>
<Delivery>2</Delivery>
<GroupMember>95</GroupMember>
</ItemGroup>
<ItemGroup id="11">
<GroupHierarchy>51</GroupHierarchy>
<Delivery>55</Delivery>
<GroupMember>100</GroupMember>
</ItemGroup>
<ItemGroup id="22">
<GroupMemberNo>95</GroupMemberNo>
<StoreName>Denver</StoreName>
<StoreID>92</StoreID>
<Staff>32</Staff>
</ItemGroup>
<ItemGroup id="52">
<StoreREF>92</StoreREF>
<StorePrice>1.99</StorePrice>
<StoreLogistics>2.00</StoreLogisics>
<ItemStored>5A</ItemStored>
<LazyRef>Yes</LazyRef>
</ItemGroup>
<ItemGroup id="22">
<GroupMemberNo>95</GroupMemberNo>
<StoreName>Alaska</StoreName>
<StoreID>22</StoreID>
<Staff>2</Staff>
</ItemGroup>
<ItemGroup id="52">
<StoreREF>22</StoreREF>
<StorePrice>2.99</StorePrice>
<StoreLogistics>4.00</StoreLogisics>
<ItemStored>1A</ItemStored>
<LazyRef>Mixed</LazyRef>
</ItemGroup>
<ItemGroup id="22">
<GroupMember>100</GroupMember>
<StoreName>Washington</StoreName>
<StoreID>34</StoreID>
<Staff>2</Staff>
</ItemGroup>
<ItemGroup id="52">
<StoreREF>34</StoreREF>
<StorePrice>2.99</StorePrice>
<StoreLogistics>4.00</StoreLogisics>
<ItemStored>1A</ItemStored>
<LazyRef>Mixed</LazyRef>
</ItemGroup>
<ItemGroup id="90">
<Misc>No additional information</Misc>
</ItemGroup>
</DocTree>

我想知道是否可以根据这些元素的ItemGroup id号来验证它们。我不能使用xsd 1.1,因为我必须使用JAXB来处理数据。此外,XML是固定的,所以我无法对其进行重组。你会注意到数据实际上是相互链接的,验证这一点会很好,但这不是绝对必要的,目前只验证基于ItemGroups的元素是很好的。

下面是对这个问题的补充,但没有回答。这是一个建议的xsd解决方法,但避免了基于InfoGroup id进行验证,并且应该允许每个属性出现多次/0。

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://wolfedgx.com/doc-list/1.0/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="DocTree" type="ns:DocTree" xmlns:ns="http://wolfedgx.com/doc-list/1.0/"/>
<xs:complexType name="DocDetails">
<xs:sequence>
<xs:element type="xs:string" name="Title" maxOccurs="unbounded" minOccurs="0" />
<xs:element type="xs:string" name="Author" maxOccurs="unbounded" minOccurs="0" />
</xs:sequence>
</xs:complexType>
<xs:complexType>
<xs:element name="InfoGroup">
<xs:sequence>
<xs:element type="xs:int" name="GroupCategory" maxOccurs="unbounded" minOccurs="0" />
<xs:element type="xs:string" name="Genre" maxOccurs="unbounded" minOccurs="0" />
<xs:element type="xs:int" name="GroupHierarchy" maxOccurs="unbounded" minOccurs="0" />
<xs:element type="xs:int" name="Delivery" maxOccurs="unbounded" minOccurs="0" />
<xs:element type="xs:int" name="GroupMember" maxOccurs="unbounded" minOccurs="0" />
<xs:element type="xs:int" name="GroupMemberNo" maxOccurs="unbounded" minOccurs="0" />
<xs:element type="xs:string" name="StoreName" maxOccurs="unbounded" minOccurs="0" />
<xs:element type="xs:int" name="StoreID" maxOccurs="unbounded" minOccurs="0" />
<xs:element type="xs:int" name="Staff" maxOccurs="unbounded" minOccurs="0" />
<xs:element type="xs:int" name="StoreREF" maxOccurs="unbounded" minOccurs="0" />
<xs:element type="xs:float" name="StorePrice" maxOccurs="unbounded" minOccurs="0" />
<xs:element type="xs:float" name="StoreLogistics" maxOccurs="unbounded" minOccurs="0" />
<xs:element type="xs:string" name="ItemStored" maxOccurs="unbounded" minOccurs="0" />
<xs:element type="xs:string" name="LazyRef" maxOccurs="unbounded" minOccurs="0" />
<xs:element type="xs:string" name="Misc" maxOccurs="unbounded" minOccurs="0" />
</xs:sequence>
<xs:attribute type="xs:int" name="id"/>
</xs:element>
</xs:complexType>
</xs:schema>

共生约束的经典示例。这需要XSD 1.1。

JAXB实际上只适用于具有高度规则和稳定结构的XML。在使用JAXB处理XML之前,请考虑使用XSLT将其转换为其他内容。

最新更新