首页 > 解决方案 > 在 DFDL 中打破循环

问题描述

我正在尝试使用 DFDL 将 FLAT 文件转换为 XML。它具有以下格式:每个元素是 5 字节。所有元素都在同一行,但我将它们分开以避免混淆。我将通过其中的第一个字母来处理元素。

0AAAA  
81AAA  
eeeee  
qqqqq    
82BBB    
rrrrr  
sssss  
9QQQQ  

现在 0 和 9 是祖父母,我们不必担心他们。8 是父级,81AAA的第二个字节(即 1)将确定其子级的格式。可以有许多 8 和 8 父级的许多子级(但它们都将具有相同的格式)。
我尝试了一种模式,但是一旦它进入儿童(eeeee),它就不会从中出来,并且每条记录都仅以儿童格式打印。

标签: dfdl

解决方案


下面是我认为描述您的数据的模式,在 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>

推荐阅读