I am having some issues with consuming an XML and applying multiple conditions on it. I have an input XML that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<results xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ResultsType">
<result>
<resultSets>
<resultSet>
<row>
<column1>11111</column1>
<column2>0</column2>
<column3>imageId/111111</column3>
<column4>2012-04-03T10:11:22.187</column4>
</row>
<row>
<column1>11111</column1>
<column2>2</column2>
<column3>imageId/111112</column3>
<column4>2012-04-03T10:11:22.187</column4>
</row>
<row>
<column1>11111</column1>
<column2>2</column2>
<column3>imageId/111113</column3>
<column4>2012-04-03T10:11:22.187</column4>
</row>
<row>
<column1>22222</column1>
<column2>0</column2>
<column3>imageId/222222</column3>
<column4>2012-04-03T10:11:22.187</column4>
</row>
<row>
<column1>22222</column1>
<column2>2</column2>
<column3>imageId/222223</column3>
<column4>2012-04-03T10:11:22.187</column4>
</row>
</resultSet>
</resultSets>
</result>
</results>
However i would like it to look like this:
<?xml version="1.0" encoding="UTF-8"?>
<results xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ResultsType">
<result>
<row>
<id>11111</id>
<lagrgeImage>imageId/111111</lagrgeImage>
<smallImage>imageId/111112</smallImage>
<smallImage>imageId/111113</smallImage>
</row>
<row>
<id>22222</id>
<lagrgeImage>imageId/222222</lagrgeImage>
<smallImage>imageId/222223</smallImage>
</row>
</result>
</results>
As you can see there are two filtering condition:
If column2 = 0 then largeImage tag is needed in the output however column2 = 2 then smallImage tag is needed in the output.
UPDATE
Both of the examples below worked perfectly, however they are both including namespacing in the root that are unexpected. The output i get is:
<?xml version="1.0" encoding="utf-8"?>
<results xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ResultsType">
<result>
<row>
<id>11111</id>
<largeImage>imageId/111111</largeImage>
<smallImage>imageId/111112</smallImage>
<smallImage>imageId/111113</smallImage>
</row>
<row>
<id>22222</id>
<largeImage>imageId/222222</largeImage>
<smallImage>imageId/222223</smallImage>
</row>
</result>
</results>
How do i remove xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ResultsType" from the above output?
If you are using XSLT2.0 you make use of the for-each-group function.
Assuming your context is resultSets this would group the rows by the ‘id’ in column1. The current grouping key could then be obtained as follows:
And to get the various rows in the group, to transform them to either largeImage or smallImage, you would do this
Here is the full XSLT
When applied to your sample XML, the following is output