TextParser Library
Working with TextParser / Using XML Template / Advanced Template
In This Topic
    Advanced Template
    In This Topic

    The Template-Based extractor also supports advanced templates with a feature called template injection, that allows reusing a defined template element multiple times within the same template. The template element can be injected anywhere in the template using its name which also helps in reducing the overall size of the template.

    Following are the pre-requisites for injecting a template element within another template element:

    Following properties need to be considered while using template injection: 

    Let's take an example to understand the template injection feature and its properties.

    Following drop down section displays the text input source:

    Click here to see the input

    Hi John,

    Following are the personal details of the new joinees.

      name = "Owen Hans",
      age = 24,
      residency address {country: "Portugal", zip code: 47007, street: "Rua da Veiga", door number: 64} ,
      work address {country: "France", zip code: 61200, street: "Rue aux chats", door number: 38}, owen64@hotmail.com ,
      phone numbers: 9169083840 9233438121 9338731232

    The following people have also been recruited and they will be joining us shortly..

      name = "Robert King", 
      residency address {country: "Nueva York", zip code: 10128, street: "Walnutwood Street", door number: 180} ,
      age = 25,phone numbers: 9064083841 9211437124,
      work address {country: "Nueva York", zip code: 10009, street: "Berkshire St.", door number: 45}

    Regards,
    Lucy

    Let's say, we create a template element ‘AddressStruct’ that will represent the address from this text input. Now, we want to use ‘AddressStruct’ template element multiple times inside another template element ‘EmployeeStruct’ to define and extract the working and residency address of an employee. So, instead of defining the ‘AddressStruct’ element two times in the same template, we can define it only once and then refer it multiple times through template injection.

    The following advanced template definition defines how we can use template injection where the ‘AddressStruct’ template element is injected in the ‘EmployeeStruct’ template element.

    <template rootElement="EmployeeStruct">
     
      <element name ="AddressStruct" startingRegex="{" endingRegex="}" childrenSeparatorRegex=",">
        <element name="country" startingRegex="country\s*:" extractFormat="quotedString"/>
        <element name="zipcode" startingRegex="zip code\s*:" extractFormat="regex:[0-9]{5}-?([0-9]{4})?"/>
        <element name="street" startingRegex="street\s*:" extractFormat="quotedString"/>
        <element name="door number" startingRegex="door number\s*:" extractFormat="int"/>
      </element>
      
      <element name="EmployeeStruct" childrenSeparatorRegex=",">
        <element name="name" startingRegex="name\s*=" extractFormat="quotedString"/>
        <element name="age" startingRegex="age\s*=" extractFormat="int"/>
        <element name="residency address" startingRegex="residency address">
          <element template="AddressStruct"/>
        </element>
        <element name="work address" startingRegex="work address">
          <element template="AddressStruct"/>
        </element>
        <element name="phone numbers" startingRegex="phone numbers\s*:">
          <element name="phone number" extractFormat="regex:[0-9]{10}" occurs="1-*"/>
        </element>
        <element name="email" extractFormat="email" occurs="0-1"/>
      </element>
     
    </template>
    

    From the above template definition, we can observe the following points:

    Note: In template injection, the change is required only in template definition. The approach for setting up and using the TemplateBasedExtractor class remains the same.

    Following image shows the parsed result in JSON string format after applying the template:

    Template injection