Add XML Icon Add XML

Description

The Add XML transform allows you to encode the content of a number of fields in a row in XML. This XML is added to the row in the form of a String field.

Options

Content Tab

Option Description

Transform name

Name of the transform.

Encoding

The encoding to use; this encoding is specified in the header of the XML file

Output Value

The name of the new field that contains the XML

Root XML element

The name of the root element in the generated element

Omit XML header

Enable to not include the XML header in the output.

Omit null values from the XML result

Do not add elements or attributes with null values. This is often used to limit the size of the target XML.

Namespaces

Namespaces are declared like any other value: add a field in the Fields tab, mark it as an attribute and give it the element name of the declaration. Because the value comes from the row, different rows can use different namespace URIs.

  • To put the root element in a default namespace, use the element name xmlns. A root element catalog with a field xmlns holding https://hop.apache.org generates <catalog xmlns="https://hop.apache.org">. Child elements without a prefix are in that namespace too.

  • To bind the root element to a prefix, name the root XML element prefix:element and declare the matching xmlns:prefix field. A root element cat:catalog with a field xmlns:cat generates <cat:catalog xmlns:cat="https://hop.apache.org">. Unprefixed child elements are then not in that namespace; give them the prefix in their element name if they should be.

  • Any further declarations, e.g. xmlns:xsi, can be added as attribute fields in the same way.

A prefixed root XML element without a matching xmlns:prefix field is an error: XML cannot use a prefix that is not bound to a namespace URI.

Fields Tab

The Fields tab is where you configure the output fields and their formats. The table below describes each of the available properties for a field.

Option Description

Fieldname

Name of the field

Element name

The name of the element in the XML file to use

Type

Type of the field can be either String, Date, or Number

Format

Select the date or number format to apply to the values in the field. See Formatting numbers and dates.

Length

Output string is padded to this length if it is specified

Precision

The number of decimal places used to display floating point numbers.

Currency

Symbol used to represent currencies like $10,000.00 or E5.000,00

Decimal

The symbol used to represent the decimal point in floating point numbers, typically "." (10.75) or "," (5,25)

Grouping

The symbol used to represent a thousands separator, typically "," (10,000.00) or "." (5.000,00)

Null

The string to use in case the field value is null.

Attribute

If Y, makes this field an attribute. Otherwise, makes it an element.

Attribute parent name

You can specify the name of the parent element to add the attribute to if previous parameter attribute is set to Y. If no parent name is specified, the attribute is set in the parent element.

Use case

Below is data that comes in a variety of classes and which needs to be stored as XML in a database. You want to turn the raw data into the database layout below.

Raw data

Shape Colour Id X Y Radius

circle

blue

1

3

5

5

circle

red

2

1

3

5

circle

blue

5

5

9

5

circle

blue

6

8

2

5

circle

red

7

9

7

5

Shape Colour Id X Y Length Width

rectangle

blue

3

3

1

6

4

rectangle

red

6

2

4

6

4

rectangle

blue

10

8

2

6

4

rectangle

red

12

7

8

6

4

rectangle

blue

14

5

2

6

4

Output Sample

Id X Y Class data

3

4

7

<SHAPE type="circle"><COLOUR>blue</COLOUR><RADIUS> 5</RADIUS></SHAPE>

1

6

3

<SHAPE type="rectangle"><COLOUR>blue</COLOUR><WIDTH> 4</WIDTH><LENGTH> 6</LENGTH></SHAPE>

2

8

8

<SHAPE type="rectangle"><COLOUR>blue</COLOUR><WIDTH> 4</WIDTH><LENGTH>6</LENGTH></SHAPE>

5

5

2

<SHAPE type="circle"><COLOUR>blue</COLOUR><RADIUS> 5</RADIUS></SHAPE>