我正在做一个项目(由一个“尖头发的经理”分配给我),我遇到了两件让我难过的事情。我已经在这里和 w3schools 上阅读了许多文章 - 仍然感到困惑。我想我需要一个实际的例子。
我包括一个显着缩减和混淆的 XSD 版本,我拼命想要“验证”它。
问题#1(我认为)与使用'abstract/substitutionGroup'有关-对我来说不是很清楚。我需要的是 3 个变体组具有相同的节点名称(在本例中为“zzzResult”)。
问题 #2与多态性有关。我需要添加特殊的“标记”来指示某些外部操作(使用 XPath 和 JS)的开始/结束以及基于名为“hash”的主文档属性(或者更确切地说是其枚举的值)的特殊数据节点。
有人可以提供一个“更正”的版本吗?
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- payload containers -->
<xsd:element name="thePayload" type="xsd:string"/>
<xsd:element name="md5">
<xsd:simpleType>
<xsd:restriction base="xsd:hexBinary">
<xsd:pattern value="[0-9A-Fa-f]{32}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="sha1">
<xsd:simpleType>
<xsd:restriction base="xsd:hexBinary">
<xsd:pattern value="[0-9A-Fa-f]{40}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<!-- markers -->
<xsd:element name="zzzBegin"><xsd:complexType/></xsd:element>
<xsd:element name="zzzEnd"><xsd:complexType/></xsd:element>
<!-- constructs -->
<xsd:element name="theHeading">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="thePayload" maxOccurs="8"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="theDetails">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="thePayload" maxOccurs="64"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<!-- groups -->
<xsd:group name="_none">
<xsd:sequence>
<xsd:element ref="theHeading"/>
<xsd:element ref="theDetails"/>
</xsd:sequence>
</xsd:group>
<xsd:group name="_md5">
<xsd:sequence>
<xsd:element ref="zzzBegin"/>
<xsd:element ref="theHeading"/>
<xsd:element ref="theDetails"/>
<xsd:element ref="zzzEnd"/>
<!-- problem #1) [abstract?] ambiguous name -->
<xsd:element name="zzzResult">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="md5"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:group>
<xsd:group name="_sha1">
<xsd:sequence>
<xsd:element ref="zzzBegin"/>
<xsd:element ref="theHeading"/>
<xsd:element ref="theDetails"/>
<xsd:element ref="zzzEnd"/>
<!-- problem #1) [abstract?] ambiguous name -->
<xsd:element name="zzzResult">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="sha1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:group>
<xsd:group name="_both">
<xsd:sequence>
<xsd:element ref="zzzBegin"/>
<xsd:element ref="theHeading"/>
<xsd:element ref="theDetails"/>
<xsd:element ref="zzzEnd"/>
<!-- problem #1) [abstract?] ambiguous name -->
<xsd:element name="zzzResult">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="md5"/>
<xsd:element ref="sha1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:group>
<!-- XML DOCUMENT -->
<xsd:element name="theDocument">
<xsd:complexType>
<!-- problem #2) [polymorphic?] choose based on enum-value of attrib "hash" -->
<xsd:choice>
<xsd:group ref="_none"/>
<xsd:group ref="_md5"/>
<xsd:group ref="_sha1"/>
<xsd:group ref="_both"/>
</xsd:choice>
<xsd:attribute name="hash" default="none">
<xsd:simpleType>
<xsd:restriction base="xsd:NMTOKEN">
<xsd:enumeration value="none"/>
<xsd:enumeration value="md5"/>
<xsd:enumeration value="sha1"/>
<xsd:enumeration value="both"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
这是我需要的变体形式:
<theDocument hash="none">
<theHeading>
<thePayload>some data here (occurs 1:8)</thePayload>
</theHeading>
<theDetails>
<thePayload>more data here (occurs 1:64)</thePayload>
</theDetails>
</theDocument>
<theDocument hash="md5">
<zzzBegin/>
<theHeading>
<thePayload>some data here (occurs 1:8)</thePayload>
</theHeading>
<theDetails>
<thePayload>more data here (occurs 1:64)</thePayload>
</theDetails>
<zzzEnd/>
<zzzResult>
<md5>0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a</md5>
</zzzResult>
</theDocument>
<theDocument hash="sha1">
<zzzBegin/>
<theHeading>
<thePayload>some data here (occurs 1:8)</thePayload>
</theHeading>
<theDetails>
<thePayload>more data here (occurs 1:64)</thePayload>
</theDetails>
<zzzEnd/>
<zzzResult>
<sha1>3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e</sha1>
</zzzResult>
</theDocument>
<theDocument hash="both">
<zzzBegin/>
<theHeading>
<thePayload>some data here (occurs 1:8)</thePayload>
</theHeading>
<theDetails>
<thePayload>more data here (occurs 1:64)</thePayload>
</theDetails>
<zzzEnd/>
<zzzResult>
<md5>0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a</md5>
<sha1>3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e</sha1>
</zzzResult>
</theDocument>