Log out?

Exercise 2 – XML to XML with XPath function

This guide is going to be shorter – I’m assuming you know what you are doing now, at least for the simple tasks we are performing. Open up the eiConsole, and create a new route. Select this chapter directory as your input listener, and use outputone as your file name input. Select this chapter directory as your transport directory, and use a descriptive title.

We don’t need a source transformation – our input file is already in XML. Create a transformation anyways, as the program requires one, just don’t do anything to the settings.

So jump straight to Target Transform.

Add a format, disable Direct Relay, and click edit. Import outputone as your source data example – it is an XML file, so it shouldn’t cause you any trouble.

Can’t find it? – you’ll notice you have to view all file types, because it is .txt, rather than .xml. This is intentional. Files are not always formatted the way you expect them to be.

Use alien2.xml as your output file example.

Viewing alien2.xml’s tags, you’ll discover immediately that there is not a good correlation between these data types. The only data item that is the same between the two data sets is race – and alien2.xml has an additional data item that needs to be filled.

Let’s deal with it first.

Map the data as you normally would – using data from the left as the input data set, then data from the target as the output data set, then alien count under data, and then a for-each structure to include the individual aliens and their respective data.

Under alienCount, we’re going to use an XPath function called Count. Go to the XPath function list and drag it down onto alienCount. Switch to the XSLT view. Pretty code, eh? Now find the

<xsl:value-of select="count()" />

tag, and fill in count() with aliens/alien, so it looks like thus:

<xsl:value-of select="count(aliens/alien)" />

We have just told the code to perform a function to count the number of incidences of “alien” items under “aliens.” Go and test it – it should return a value of 4. Our other tags are empty, however – let’s fill them.

Race is quick and easy – just drag race over from the source data. One-to-one mapping.

The first letter of the name is similar to count – you need to switch over to XSLT after dropping a substring function from XPath, and fill in the function parameters. I leave this to you as an exercise.

Age group is somewhere more difficult – for this example, use nested If functions – you can do it other ways, but this time we’ll do it this way, using the Text function. o

Your final results in the testing state should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<data>
        <alienCount>4</alienCount>
	<alien>
		<firstLetterOfName>z</firstLetterOfName>
		<agegroup>youngAlien</agegroup>
		<race>pogi</race>
	</alien>
	<alien>
		<firstLetterOfName>t</firstLetterOfName>
		<agegroup>teenAlien</agegroup>
		<race>ribbi</race>
	</alien>
	<alien>
		<firstLetterOfName>i</firstLetterOfName>
		<agegroup>teenAlien</agegroup>
		<race>pop</race>
	</alien>
	<alien>
		<firstLetterOfName>w</firstLetterOfName>
		<agegroup>babyAlien</agegroup>
		<race>wonk</race>
	</alien>
</data>

Save, and test your route. It should produce exactly the same data.

Spend some time experimenting with the different functions.

    to post comments

    This is a unique website which will require a more modern browser to work! Please upgrade today!

    Thanks!

    Our editors are notified.

    Close