XML Generator

Overview

This Snap generates a new XML document for the next Snap in the pipeline. The Snap will pass-through the input data if an input view is provided. The output of the Snap will provide an XML attribute in the document which provides the serialized XML content as a string.



Snap views

View Description Examples of upstream and downstream Snaps
Input This Snap has at most one document input view.

Document that conforms to variables needed for the variable substitutions of the XML template.

Output This Snap has exactly one document output view.

Document which provides the results of the XML template substitution. Every input document (if an input view is provided) will result in a one output document which provides the result of the XML template substitution under the XML attribute of the document.

Error

Error handling is a generic way to handle errors without losing data or failing the Snap execution. You can handle the errors that the Snap might encounter when running the pipeline by choosing one of the following options from the When errors occur list under the Views tab. The available options are:

  • Stop Pipeline Execution Stops the current pipeline execution when an error occurs.
  • Discard Error Data and Continue Ignores the error, discards that record, and continues with the remaining records.
  • Route Error Data to Error View Routes the error data to an error view without stopping the Snap execution.

Learn more about Error handling in Pipelines.

Snap settings

Legend:
  • Expression icon (): Allows using pipeline parameters to set field values dynamically (if enabled). SnapLogic Expressions are not supported. If disabled, you can provide a static value.
  • SnapGPT (): Generates SnapLogic Expressions based on natural language using SnapGPT. Learn more.
  • Suggestion icon (): Populates a list of values dynamically based on your Snap configuration. You can select only one attribute at a time using the icon. Type into the field if it supports a comma-separated list of values.
  • Upload : Uploads files. Learn more.
Learn more about the icons in the Snap settings dialog.
Field / Field set Type Description
Label String

Required. Specify a unique name for the Snap. Modify this to be more appropriate, especially if more than one of the same Snaps is in the pipeline.

Default value: XML Generator

Example: XML Generator
Inbound schema String/Expression XML schema definition of the incoming data.
Schema Xpath String

The path for the schema object in the provided schema file, which must be used for validating the XML data. This is required if the schema object is nested within a wrapper object or within an object further in the schema file.

See example Validating XML Data Using the Schema XPath for more information on using the Schema XPath field.

XML root element String/Expression Root element for the XML generation.
Validate XML Checkbox

Appears when you specify an XSD schema definition in the Inbound schema field.

Select this checkbox to validate the incoming data against the specified XSD schema definition.

Important: If you enter an Inbound schema, then you must select Validate XML and Match data types properties to derive the output as per the defined schema.

Default status: Deselected

Escape special characters Checkbox
Select this checkbox to escape XML special characters in XML template variable values when you insert the values into the Apache Velocity template. For example, the Snap replaces < with &It;. The special characters that the Snap escapes are:
  • >
  • <
  • &
  • '
  • "

XML Generator normalizes all attributes to use double quotes for the delimiters, even if the source content uses single quotes. Therefore, if there are double quotes that appear inside the attributes, then they have to be escaped.

Learn more: HandlingSpecialCharacters.

Default status: Deselected

Default value for substitution String This value will be used for substitution for the leaf elements which are not objects and/or not enclosed in an array. If nothing is specified and Use default value for substitution is selected, an empty string will be used for substitution.
Use default value for substitution Checkbox If you select this check box, the value provided for the Default value for substitution is used for substitution if the elements do not exist in the incoming documents. Otherwise, the elements for which the incoming documents do not have any values will be deleted from the XML.

Default status: Deselected

Namespace Context

Namespace context for schema element.

Prefix String Prefix of schema element in the provided schema file.
Namespace URI String Namespace of schema element in the provided schema file.
Edit XML Button This property lets you edit the XML contents. The elements to be generated in the output must be specified in the XML template. If your output does not display the required elements, click Edit XML and specify the applicable elements in the Generate Template tab. You can use the Apache Velocity template to pass dynamic values from upstream Snaps.
Note: Elements specified in the schema document in the Inbound schema field do not affect the output.
Snap execution Dropdown list
Choose one of the three modes in which the Snap executes. Available options are:
  • Validate & Execute: Performs limited execution of the Snap and generates a data preview during pipeline validation. Subsequently, performs full execution of the Snap (unlimited records) during pipeline runtime.
  • Execute only: Performs full execution of the Snap during pipeline execution without generating preview data.
  • Disabled: Disables the Snap and all Snaps that are downstream from it.

Default value: Validate and Execute

Example: Execute only

Handling Special Characters

The following are the basic rules when the special characters are to be escaped:
  • If the special characters are inside the element text, then they are not to be escaped.

  • If the special characters are inside the attribute values that are delimited by single or double quotes, then they are to be escaped.

  • If the special characters and the delimiter being used for that attribute are the same, then they are to be escaped.

The following table lists a few examples of how to escape special characters:
Example Attribute/Element in XML Description
He said "OK" attributeName="He said &quot;OK&quot;" The double quotes must be escaped.
He said "OK" attributeName='He said "OK"' The double quotes need not be escaped as they are contained within a single-quoted attribute.
She said "You're right" attributeName="She said &quot;You're right&quot;" The ' (apostrophe) in You’re need not be escaped as it is contained within double quotes.
She said "You're right" attributeName='She said "You&apos;re right"' The double quotes need not be escaped as they are contained within a single-quoted attribute. However, you must escape the apostrophe in You’re.
She said "You're right" attributeName="She said &quot;You&apos;re right&quot;" To escape all the data.
Smith&Sons attributeName="Smith&amp;Sons" The & must always be escaped within attribute data.

a>b

a<b

attributeName="a&gt;b"

attributeName="a&lt;b"

It is good practice to escape > or < characters, though not mandatory if they are used in an attribute.

if (age < 5)

if (age > 5)

if (age > 3 && age < 8)

<MyElement>if (age &lt; 5)</MyElement>

<MyElement>if (age &gt; 5)</MyElement>

<MyElement>if (age &gt; 3 &amp;&amp; age &lt; 8)</MyElement>

It is good practice to escape >, <, or & characters if they are used within an element though not mandatory.
She said "You're right" <MyElement>She said "You're right"</MyElement> The single and double quotes need not be escaped if they are within an Element

Troubleshooting

XML Formatter and XML Generator Output Differs for the Same XSD File Input

The XML Formatter and XML Generator Snaps work differently for an XSD file as the input document. To generate the same output from both the Snaps, append the following to the required schema in the Target path property of the preceding Mapper Snap:

Examples