我正在尝试使用 DFDL 和 Daffodil 将一个复杂的固定长度文件转换为 XML。每行将负责一个元素,每行的第一个元素将告诉我它将是哪种元素。它可以是父 A 或父 B,也可以是子 AA 或 AB 或 BB 或 BA。
其中父 A 是一个元素,父 B 是另一个元素,子 AA 是元素 A 的第一个子元素。
在一个文件中,有多个父 A 和父 B。我尝试了启动器标签甚至尝试了选择标签,但似乎没有任何效果。谁能帮帮我。
没有示例数据很难给出完整的答案,但使用发起者和选择可能是正确的方法。根据特定数据,可能存在更简单的模式,但通用解决方案可能如下所示:
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
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" lengthKind="delimited" />
</xs:appinfo>
</xs:annotation>
<xs:element name="File">
<xs:complexType>
<xs:sequence>
<xs:element name="Record" maxOccurs="unbounded">
<xs:complexType>
<xs:choice dfdl:initiatedContent="yes">
<xs:element name="ParentA" dfdl:initiator="ParentA:">
<xs:complexType>
<xs:sequence dfdl:separator="%NL;" dfdl:separatorPosition="postfix">
<xs:element name="Content" type="xs:string"/>
<xs:element name="Record" maxOccurs="unbounded">
<xs:complexType>
<xs:choice dfdl:initiatedContent="yes">
<xs:element name="ChildAA" type="xs:string" dfdl:initiator="ChildAA:" />
<xs:element name="ChildAB" type="xs:string" dfdl:initiator="ChildAB:" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ParentB" dfdl:initiator="ParentB:">
<xs:complexType>
<xs:sequence dfdl:separator="%NL;" dfdl:separatorPosition="postfix">
<xs:element name="Content" type="xs:string" />
<xs:element name="Record" maxOccurs="unbounded">
<xs:complexType>
<xs:choice dfdl:initiatedContent="yes">
<xs:element name="ChildBA" type="xs:string" dfdl:initiator="ChildBA:" />
<xs:element name="ChildBB" type="xs:string" dfdl:initiator="ChildBB:" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
该架构具有以下特点:
上面允许这样的数据:
ParentA:Parent A Content
ChildAA:Child AA Content
ChildAB:Child AB Content
ParentB:Parent B Content
ChildBB:Child BB Content
ParentA:Parent A Content
ChildAB:Child AB Content
它将解析成这样的 XML 信息集:
<File>
<Record>
<ParentA>
<Content>Parent A Content</Content>
<Record>
<ChildAA>Child AA Content</ChildAA>
</Record>
<Record>
<ChildAB>Child AB Content</ChildAB>
</Record>
</ParentA>
</Record>
<Record>
<ParentB>
<Content>Parent B Content</Content>
<Record>
<ChildBB>Child BB Content</ChildBB>
</Record>
</ParentB>
</Record>
<Record>
<ParentA>
<Content>Parent A Content</Content>
<Record>
<ChildAB>Child AB Content</ChildAB>
</Record>
</ParentA>
</Record>
</File>
以上是用 Apache Daffodil 2.2.0 测试的