首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在DFDL中循环

在DFDL中循环
EN

Stack Overflow用户
提问于 2018-10-12 11:07:40
回答 1查看 561关注 0票数 0

我正在尝试使用DFDL和Daffodil将DFDL复杂的固定长度文件转换为XML。每一行将负责一个元素,每一行的第一个元素将告诉我它将是什么样的元素。它可以是父母A或父母B,也可以是子女AA或AB或BB或BA。

如果父A是一个元素,父B是另一个元素,子AA是元素A的第一个子元素。

在一个文件中,有多个父文件A和父文件B。我尝试了启动器标记,甚至尝试了选择标记,但是似乎没有什么效果。有人能帮帮我吗。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-12 13:25:15

如果没有示例数据,很难给出完整的答案,但是使用发起者和选择可能是正确的方法。根据特定的数据,有一些可能更简单的模式,但是一般的解决方案可能如下所示:

代码语言:javascript
复制
<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>

此模式具有以下特性:

  • 每个文件都有一个无限制的记录数。
  • 每个记录都是由dfdl:启动器属性确定的ParentA或ParentB元素的选择。
  • 每个父元素都包含该父元素的内容(即父启动程序后面的内容),后面是无限数量的子记录。
  • 每个子记录也由dfdl:initator属性确定。
  • 后缀换行符用于确定父内容和子内容何时结束。
  • 这不允许ChildB元素出现在ParentA元素之后,反之亦然--子元素必须始终出现在关联的父元素之后。(如果此限制不重要,则可以大大简化此模式)。

上面允许这样的数据:

代码语言:javascript
复制
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信息集:

代码语言:javascript
复制
<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进行了测试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52778239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档