Elements: Controlling the order and the repetition (DataStage®)
Controlling the repetition of nested elements
In a given XML hierarchy, nested elements may repeat. The following example contains a list of cities in which a customer operates.
<directory>
<customer name="Acme">
<city>Boston</city>
<city>Chicago</city>
<city>New York</city>
On the input link, city values map to the hierarchy using the following XPath expression:
/directory/customer/city/text()
To
enable the <city>
element to repeat within the
<customer>
node, you need to define this XPath expression as the repetition
path. To do this, identify the associated input column as the key.
Repetition applies to each node beneath the root element. Therefore, the
<customer>
element can also repeat. The final output typically depends on the
repetition path and the order of the input rows. Two repetition paths can produce the same
results.
Controlling the order of elements
If you want the XML document to conform
to a schema that stipulates the sequence of elements (<xs:sequence>
), you
must order the XPath expressions on the input link accordingly. XML Output sets the position of an
element based on its first occurrence in the set of XPath expressions.
The repetition path /directory/customer/city/text()
is in effect,
and the order of XPath expressions is as follows. Notice that the first occurrence of the node
division
precedes the first occurrence of the node city
.
Column | XPath expression |
---|---|
CUSTOMER |
/directory/customer/@name |
DIVISION |
/directory/customer/division/text() |
CITY |
/directory/customer/city/text() |
The input data consists of four rows:
CUSTOMER | DIVISION | CITY |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Because the node division
precedes city
, the
output is as follows:
<directory>
<customer name="Acme">
<division>Toys</division>
<city>Boston</city>
<city>New York</city>
</customer>
<customer name="Acme">
<division>Chemical</division>
<city>Boston</city>
<city>New York</city>
</customer>
</directory>
If you reorder the XPath expressions, the result changes. For example, the XPath
expression for CITY
is now in the middle position:
Column | XPath expression |
---|---|
CUSTOMER |
/directory/customer/city/text() |
CITY |
/directory/customer/city/text() |
DIVISION |
/directory/customer/division/text() |
This reordering generates the following output:
<directory>
<customer name="Acme">
<city>Boston</city>
<city>New York</city>
<division>Toys</division>
</customer>
<customer name="Acme">
<city>Boston</city>
<city>New York</city>
<division>Toys</division>
</customer>
</directory>