下面是我认为描述您的数据的模式,在 Daffodil 2.2.0 上进行了测试:
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/">
<xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd" />
<xs:annotation>
<xs:appinfo source="http://www.ogf.org/dfdl/">
<dfdl:format ref="GeneralFormat" />
</xs:appinfo>
</xs:annotation>
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="GrandParent" maxOccurs="unbounded">
<xs:complexType>
<xs:choice dfdl:initiatedContent="yes">
<xs:element name="Zero" dfdl:initiator="0">
<xs:complexType>
<xs:sequence>
<xs:element name="Value" type="xs:string" dfdl:length="4" dfdl:lengthKind="explicit" />
<xs:element ref="Eight" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Nine" dfdl:initiator="9">
<xs:complexType>
<xs:sequence>
<xs:element name="Value" type="xs:string" dfdl:length="4" dfdl:lengthKind="explicit" />
<xs:element ref="Eight" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Eight" dfdl:initiator="8">
<xs:complexType>
<xs:sequence>
<xs:element name="ChildrenFormat" type="xs:string" dfdl:length="1" dfdl:lengthKind="explicit" />
<xs:element name="Value" type="xs:string" dfdl:length="3" dfdl:lengthKind="explicit" />
<xs:choice dfdl:choiceDispatchKey="{ ./ChildrenFormat }">
<xs:element ref="One" maxOccurs="unbounded" dfdl:choiceBranchKey="1" />
<xs:element ref="Two" maxOccurs="unbounded" dfdl:choiceBranchKey="2" />
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="One" type="xs:string" dfdl:length="5" dfdl:lengthKind="explicit">
<xs:annotation>
<xs:appinfo source="http://www.ogf.org/dfdl/">
<dfdl:discriminator test="{ fn:not(fn:starts-with(., '8') or fn:starts-with(., '9')) }" />
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element name="Two" type="xs:string" dfdl:length="5" dfdl:lengthKind="explicit">
<xs:annotation>
<xs:appinfo source="http://www.ogf.org/dfdl/">
<dfdl:discriminator test="{ fn:not(fn:starts-with(., '8') or fn:starts-with(., '9')) }" />
</xs:appinfo>
</xs:annotation>
</xs:element>
</xs:schema>
这是如何工作的描述:
- 数据的根是无限数量的 GrandParent 元素
- 每个 GrandParent 元素都包含零或九,具体取决于发起者。发起者使用祖父母数据的 5 个字节中的第一个
- 零/九元素包含一个消耗剩余 4 个字节的渐变数据的值
- 后面的值是零个或多个八个元素
- 每个Eight元素都有一个“8”的发起者,消耗5个字节中的第一个
- 每个 8 元素都有一个 ChildrenFormat,占用 5 个字节中的第二个
- 每个 8 元素都有一个值,消耗 5 个字节中的最后 3 个
- 每个八元素都有无限数量的全一或全二元素
- ChoiceDispatchKey/Branch 用于确定是解析所有 One 还是所有 Two 元素,调度 ChildrenFormat 元素
- 每个 One 或 Two 元素占用 5 个字节
- 为了确定无限数量的 One 或 Two 元素何时结束,在 One/Two 元素上放置一个鉴别器。当解析为 One/Two 的数据不是以“8”或“9”开头时,此鉴别器会失败。
- 此外,为简单起见,所有字段都被视为字符串
这样,您的示例数据将解析为如下信息集:
<Root>
<GrandParent>
<Zero>
<Value>AAAA</Value>
<Eight>
<ChildrenFormat>1</ChildrenFormat>
<Value>AAA</Value>
<One>eeeee</One>
<One>qqqqq</One>
</Eight>
<Eight>
<ChildrenFormat>2</ChildrenFormat>
<Value>BBB</Value>
<Two>rrrrr</Two>
<Two>sssss</Two>
</Eight>
</Zero>
</GrandParent>
<GrandParent>
<Nine>
<Value>QQQQ</Value>
</Nine>
</GrandParent>
</Root>