Search code examples
xmlxsdbiztalkflat-filebiztalk-2013

BizTalk - Build XSD for flat file with words delimiter at start and end of records


I am trying to build an XSD file based on the following text file (flat file). I am using the flat file schema wizard but can't achieve what I want.

:BEGIN
Key1 Value1
Key2 Value2
:END
:BEGIN
Key1 Value1
Key2 Value2
Key3 Value3
:END
:BEGIN
:END

The XSD would be something like :

Record (Delimited by :BEGIN and :END)
|_Association (Delimited by \n)
  |_Key (Delimited by space, first occurence)
  |_Value (second occurence)

I would like my txt file to be transformed as an instance of the xsd file in xml. The result would be something like :

<Record>
  <Association>
    <Key>Key1</Key>
    <Value>Value1</Value>
  </Association>
  <Association>
    <Key>Key2</Key>
    <Value>Value2</Value>
  </Association>
</Record>
<Record>
  <Association>
    <Key>Key1</Key>
    <Value>Value1</Value>
  </Association>
  <Association>
    <Key>Key2</Key>
    <Value>Value2</Value>
  </Association>
  <Association>
    <Key>Key3</Key>
    <Value>Value3</Value>
  </Association>
</Record>

There is only 2 records because last :BEGIN:END block is empty.

My main issue is that I can't find out how to delimit my records by :START and :END tags. Is that even possible ?


Solution

  • Yes, it is possible to do, but not with the Wizard. It is a matter of manually building it up and testing the structure as you add definitions.

    It is a matter of using Tag Identifiers for the :BEGIN and :END as well using a Sequence Group to say that these re-occur.

    Additionally at the <Schema> level set Parser Optimization to Complexity.

    Depending on what defines the end of your line, you may need to adjust the 0x0A 0x0D (Carriage Return & Line Feed) to what your file actually contains. Also if the last line does not finish with a CR/LF then change it from being Postfix (after) to Infix (between).

    <?xml version="1.0" encoding="utf-16"?>
    <xs:schema xmlns="http://Scratch.SO63600733" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" targetNamespace="http://Scratch.SO63600733" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:annotation>
        <xs:appinfo>
          <b:schemaInfo standard="Flat File" root_reference="Root" default_pad_char=" " pad_char_type="char" count_positions_by_byte="false" parser_optimization="complexity" lookahead_depth="3" suppress_empty_nodes="false" generate_empty_nodes="true" allow_early_termination="false" early_terminate_optional_fields="false" allow_message_breakup_of_infix_root="false" compile_parse_tables="false" />
          <schemaEditorExtension:schemaInfo namespaceAlias="b" extensionClass="Microsoft.BizTalk.FlatFileExtension.FlatFileExtension" standardName="Flat File" xmlns:schemaEditorExtension="http://schemas.microsoft.com/BizTalk/2003/SchemaEditorExtensions" />
        </xs:appinfo>
      </xs:annotation>
      <xs:element name="Root">
        <xs:annotation>
          <xs:appinfo>
            <b:recordInfo structure="delimited" preserve_delimiter_for_empty_data="true" suppress_trailing_delimiters="false" sequence_number="1" />
          </xs:appinfo>
        </xs:annotation>
        <xs:complexType>
          <xs:sequence>
            <xs:annotation>
              <xs:appinfo>
                <b:groupInfo sequence_number="0" />
              </xs:appinfo>
            </xs:annotation>
            <xs:element maxOccurs="unbounded" name="Record">
              <xs:annotation>
                <xs:appinfo>
                  <b:recordInfo structure="delimited" preserve_delimiter_for_empty_data="true" suppress_trailing_delimiters="false" child_order="postfix" child_delimiter_type="hex" child_delimiter="0x0D 0x0A" sequence_number="1" />
                </xs:appinfo>
              </xs:annotation>
              <xs:complexType>
                <xs:sequence>
                  <xs:annotation>
                    <xs:appinfo>
                      <b:groupInfo sequence_number="0" />
                    </xs:appinfo>
                  </xs:annotation>
                  <xs:sequence>
                    <xs:annotation>
                      <xs:appinfo>
                        <b:groupInfo sequence_number="1" />
                      </xs:appinfo>
                    </xs:annotation>
                    <xs:element name="BEGIN">
                      <xs:annotation>
                        <xs:appinfo>
                          <b:recordInfo structure="delimited" preserve_delimiter_for_empty_data="true" suppress_trailing_delimiters="false" sequence_number="1" tag_name=":BEGIN" />
                        </xs:appinfo>
                      </xs:annotation>
                      <xs:complexType />
                    </xs:element>
                    <xs:element minOccurs="0" maxOccurs="unbounded" name="Accociation">
                      <xs:annotation>
                        <xs:appinfo>
                          <b:recordInfo structure="delimited" preserve_delimiter_for_empty_data="true" suppress_trailing_delimiters="false" child_delimiter_type="char" child_order="infix" child_delimiter=" " sequence_number="2" />
                        </xs:appinfo>
                      </xs:annotation>
                      <xs:complexType>
                        <xs:sequence>
                          <xs:annotation>
                            <xs:appinfo>
                              <b:groupInfo sequence_number="0" />
                            </xs:appinfo>
                          </xs:annotation>
                          <xs:element name="Key" type="xs:string">
                            <xs:annotation>
                              <xs:appinfo>
                                <b:fieldInfo sequence_number="1" justification="left" />
                              </xs:appinfo>
                            </xs:annotation>
                          </xs:element>
                          <xs:element name="value" type="xs:string">
                            <xs:annotation>
                              <xs:appinfo>
                                <b:fieldInfo sequence_number="2" justification="left" />
                              </xs:appinfo>
                            </xs:annotation>
                          </xs:element>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="END">
                      <xs:annotation>
                        <xs:appinfo>
                          <b:recordInfo structure="delimited" preserve_delimiter_for_empty_data="true" suppress_trailing_delimiters="false" tag_name=":END" child_order="postfix" child_delimiter_type="hex" child_delimiter="0x0A 0x0D" sequence_number="3" />
                        </xs:appinfo>
                      </xs:annotation>
                      <xs:complexType />
                    </xs:element>
                  </xs:sequence>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    With your example input gives

    <Root xmlns="http://Scratch.SO63600733">
        <Record xmlns="">
            <BEGIN/>
            <Accociation>
                <Key>Key1</Key>
                <value>Value1</value>
            </Accociation>
            <Accociation>
                <Key>Key2</Key>
                <value>Value2</value>
            </Accociation>
            <END/>
        </Record>
        <Record xmlns="">
            <BEGIN/>
            <Accociation>
                <Key>Key1</Key>
                <value>Value1</value>
            </Accociation>
            <Accociation>
                <Key>Key2</Key>
                <value>Value2</value>
            </Accociation>
            <Accociation>
                <Key>Key3</Key>
                <value>Value3</value>
            </Accociation>
            <END/>
        </Record>
        <Record xmlns="">
            <BEGIN/>
            <END/>
        </Record>
    </Root>