On this Page

Snap type:

Read


Description:

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.

  • Expected upstream Snaps: A Mapper Snap which maps into the JSON schema provided by the XML Generator Snap. The provided JSON schema is derived from the XSD that can be provided for the XML Generator Snap.
  • Expected downstream Snaps: The Snap will output one document for every input document, hence and document processing Snap can be used downstream. 
  • Expected input: Document that conforms to variables needed for the variable substitutions of the XML template.
  • Expected output: 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.


Prerequisites:

[None]


Support and limitations:Works in Ultra Task Pipelines.
Account: 

Accounts are not used with this Snap.


Views:


InputThis Snap has at most one document input view.
OutputThis Snap has exactly one document output view.
ErrorThis Snap has at most one document error view and produces zero or more documents in the view.


Settings

Label


Required. The name for the Snap. You can modify this to be more specific, especially if you have more than one of the same Snap in your pipeline.

Inbound schema
 

XML schema definition of the incoming data. 

Schema XPath

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

Root element for the XML generation.


Validate XML

Required. 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.

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 Value: Deselected

Escape special characters

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: XML Generator#HandlingSpecialCharacters.

Default value: Not selected

We recommend that you select this checkbox unless your variable values contain XML Markup or the contents are already escaped.


Default value for substitution

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 substitutionIf 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.
Namespace Context

Namespace context for schema element.

Prefix

Prefix of schema element in the provided schema file.

Namespace URI

Namespace of schema element in the provided schema file.

Edit XML

Required. 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 /wiki/spaces/AP/pages/1438085 template to pass dynamic values from upstream Snaps.

Elements specified in the schema document in the Inbound schema field do not affect the output.


Handling Special Characters

The following are the basic rules when the special characters 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



XML Generation via XSD

The first example depicts a simple pipeline to generate order data XML directly with the XML Generator Snap.

 

We provide the sample XSD as defined below:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
  <xs:complexType>
 <xs:sequence>
   <xs:element name="orderperson" type="xs:string"/>
   <xs:element name="shipto">
     <xs:complexType>
       <xs:sequence>
         <xs:element name="name" type="xs:string"/>
         <xs:element name="address" type="xs:string"/>
         <xs:element name="city" type="xs:string"/>
         <xs:element name="country" type="xs:string"/>
       </xs:sequence>
     </xs:complexType>
   </xs:element>
   <xs:element name="item" maxOccurs="unbounded">
     <xs:complexType>
       <xs:sequence>
         <xs:element name="title" type="xs:string"/>
         <xs:element name="note" type="xs:string" minOccurs="0"/>
         <xs:element name="quantity" type="xs:positiveInteger"/>
         <xs:element name="price" type="xs:decimal"/>
       </xs:sequence>
     </xs:complexType>
   </xs:element>
 </xs:sequence>
 <xs:attribute name="orderid" type="xs:string" use="required"/>
  </xs:complexType>
</xs:element>
</xs:schema>


Originated from: http://www.w3schools.com/schema/schema_example.asp

 

We then suggest the XML root element, which returns {}shiporder.
Finally we click on Edit XML, which will automatically trigger the XML template generation based off the XSD, see below.

Now we could replace the variables with our own values to generate the XML on the output view or move on to the next example. 
*The execution of the Snap above will create an XML attribute on the output view which provides the serialized XML content as a string. 


Mapping to XML Generator via XSD

Let us use a JSON Generator to provide the input order data, such as defined below:

[
    {
        "items" : [{
            "title": "iphone6",
            "quantity": 1,
            "price": 598.00
        },
        {
            "title": "note 4",
            "quantity": 1,
            "price": 599.00
        }
        ],
         "address":"some address",
        "city": "San Mateo",
        "orderId": "1234",
        "name": "Mr. Undecided"
    },
    {
        "items" : [{
            "title": "iphone6",
            "quantity": 1,
            "price": 598.00
        },
        {
            "title": "note 4",
            "quantity": 1,
            "price": 599.00
        },
        {
            "title": "lumina",
            "quantity": 1,
            "price": 0.99
        }
        ],
         "address":"some address",
        "city": "San Mateo",
        "orderId": "1234",
        "name": "Mr. Even more Undecided"
    }
]
 

We then map the data using the Mapper Snap which has access to the XSD of the downstream XML Generator Snap of the previous example (now with an added input view).  


Here we map the items to the item list on the target. Further we map the city, address, country and name to the shipTo object on the target and then finally we map the name against orderperson and orderId against @orderId on the target. The @ indicates we map against an XML attribute

Let us see the output of the XML Snap.

Here we see that each incoming order document was translated into an xml string. We include the original data from the input view, in case it is further needed downstream.
The XML Generator Snap can validate the generated content if needed using the “Validate XML” property.


Writing the Generated Content to File

Sometimes one wants to write the generated XML to a file. For that use case we provide a DocumentToBinary Snap which can take the content and convert it to binary data object, which then can be written to a file, such as using a File Writer Snap.

 

Above we map XML to the content field of the DocumentToBinary Snap, and set the Encode or Decode option on the DocumentToBinary Snap to NONE.


This outputs then one binary document for each order. We can then write it to a directory. (Careful, here you’d want to use the APPEND option, since you potentially would be writing two files to the same directory or you can use an expression such as Date.now() to write individual files for each incoming binary data object).


POSTing the Generated Content

In the last example we will be POSTing the generated content to some REST endpoint using the REST POST Snap.


In the screenshot below we are using the POST Snap which has the entity set as $xml. That will use the XML content that was generated by the upstream XML Generator Snap and POST it as a body to the endpoint.
You might want to set the content-type and accept header as defined below.

 

The POST will be executed for every document on the input view. There are a total of two documents, hence we will execute two post requests. 


Validating XML Data Using the Schema XPath and Validate XML Settings

This example demonstrates how to use the Schema XPath field to successfully validate XML data.

Download the Sample Pipeline.

In this Pipeline, custom XML data is provided using the Edit XML field in the XML Generator Snap, a snapshot of which is provided here. 

The Inbound schema, Schema XPath, and the schema Root element, are specified in the Snap settings. See the screenshot below. In addition, the Validate XML check box is selected to indicate that the XML data must be validated against the schema.

The file specified as the inbound schema, CustomersOrders_withXSD.xml, is not a regular schema file (.xsd) but an XML file being used as a schema file. The contents of this file appear as follows:

In the above file, the root element of the schema is nested within the RootElm object. Therefore, the Schema XPath field value is specified as //RootElm/* to direct the XML Generator Snap to the schema object.   

On validating the Pipeline, the Snap output appears as follows. Note the ContactName field. 

If the ContactName field is removed from the custom XML specified in the XML editor and the Pipeline is re-validated, the Snap fails with the following error:

Note that if the Validate XML check box is deselected, the Snap does not validate the data against the schema, and an output with the missing field is generated without errors:

Similarly, if the Schema XPath value is not provided, the Snap is unable to locate the root element and fails with the following error: 

This explains the scenarios in which you need to provide the schema XPath and how you can validate XML data using schema from within any XML or XSD file with the help of the Schema XPath and Validate XML properties.


Custom XML Output Generation using Inbound Schema and Root Element 

This example illustrates the corrected behavior of the XML Generator Snap as of Patch transform8760, whereby the Snap does not ignore any custom XML data that is provided through its XML editor. 

Download the Sample Pipeline.

Scenario 1: When used with upstream Snaps

The sample Pipeline provided here generates XML for an Add operation. The XML output should contain two integer values: intA and intB.



Using two XML Generator Snaps: Customized XML and Generated XML, the Pipeline demonstrates the differing output when custom XML is provided vs when the output is generated using upstream data. Both Snaps are provided the same schema info.

In the Customized XML Snap, custom XML data is provided using the Edit XML field in the Snap Settings as follows:



Here, the values of intA and intB are 0 and 1 respectively.

The Generated XML Snap contains the XML that is generated through the Generate Template button in the XML editor:

In this case, the values of intA and intB are dynamically assigned when the Snap executes.

Both Snaps are fed an input document that specifies intA=2, and intB=3.

Old behavior:
The Customized XML Snap had the same output as the Generated XML Snap and ignored the custom XML data in the editor:

Current behavior:
The two Snaps have different outputs. The output of the Customized XML Snap is based on the XML provided by the user:

The output of the Generated XML Snap is based on upstream data: 

Scenario 2: When used as a standalone Snap

The sample Pipeline provided below generates XML for an Add operation.

Custom XML is provided using the Edit XML field, as follows: 

Old behavior:

Pipeline execution would result in a Premature end of file error: 

Current behavior:

Pipeline execution is successful and custom XML output is generated:

This example illustrates how you can generate custom XML output through the XML Generator Snap.  

Downloads