My XML is shown below:
<?xml version="1.0" encoding="UTF-8"?>
<JobListing>
<ColumnOrder>
<columnId>4</columnId>
<columnId>2</columnId>
<columnId>5</columnId>
<columnId>1</columnId>
<columnId>3</columnId>
</ColumnOrder>
<Jobs>
<Job>
<title>Java Developer</title>
<code>JAVA</code>
<openings>10</openings>
<location>USA</location>
<duration>6 months</duration>
</Job>
<Job>
<title>.NET Developer</title>
<code>DOTNET</code>
<openings>10</openings>
<location>USA</location>
<duration>6 months</duration>
</Job>
</Jobs>
</JobListing>
I’ve a specific requirement that Jobs should be listed in a HTML page based on ColumnOrder specified in the XML. Internally, each columnId is mapped to a column as given below:
- 1 -> Title
- 2 -> Code
- 3 -> Openings
- 4 -> Location
- 5 -> Duration
In this case, for example, Job listing page should list columns in this order – Location, Code, Duration, Title, Openings. Am expecting something like as explained below:
<tr>
loop jobs
for each columnorder
if(columnId == 1)
<td>get Title</td>
else if (columnId == 2)
<td>get Code</td>
else if (columnId == 3)
<td>get Openings</td>
else if (columnId == 4)
<td>get Location</td>
else if (columnId == 5)
<td>get Duration</td>
end if
end columnorder
end jobs
</tr>
How do I achieve this using XSLT?
Any help or different ideas/suggestions would be greatly appreciated.
EDIT: The <Job> elements in the XML (title, openings, location, duration, code) are not necessarily in the same order. In fact, in our current framework, it gets generated in ascending order (like code, duration, location, openings, title). How to make it work without taking into account order of elements of <Job>?
This transformation works even when the children of
Jobare mixed in any order:When applied on the provided XML document:
The wanted, correct result is produced: