Subject:Transforming XML into another XML. Author:Thanuja Rao Date:21 Mar 2008 01:39 AM
Hi All,
I am transforming an XML into another XML using XSLT v1.0. My input XML contains a list of questions. Each QUESTION has an attribute called TOPIC which basically mentions the section to which this question belongs to. The questions in the input XML are in a sorted order in the sense, that all questions belonging to a particular topic are together. In my output XML, I need to create an element called section which will have the TOPIC name and within that all questions of that topic need to be listed. So, basically each time the TOPIC attribute changes in the input XML, I need to add a SECTION element to the output. Ensuring that this section element is closed at the end of each section is creating a problem. Could anyone please help me?
Subject:Transforming XML into another XML. Author:Thanuja Rao Date:27 Mar 2008 12:41 AM Originally Posted: 27 Mar 2008 12:40 AM
I dont think I was clear enough in stating my requirement in my previous post. Here are some portions of the code snippets.
Input XML:
<ROOT>
<QUESTION TOPIC="ExamName1\Topic1\Complexity1">
<CONTENT> The 1st Question comes here </CONTENT>
</QUESTION>
<QUESTION TOPIC="ExamName1\Topic1\Complexity1">
<CONTENT> The 2nd Question comes here </CONTENT>
</QUESTION>
</ROOT>
Another Input XML:<!--please note the change in the topic-->
<ROOT>
<QUESTION TOPIC="ExamName1\Topic1\Complexity2">
<CONTENT> The 3rd Question comes here </CONTENT>
</QUESTION>
<QUESTION TOPIC="ExamName1\Topic1\Complexity2">
<CONTENT> The 3rd Question comes here </CONTENT>
</QUESTION>
</ROOT>
I am using XSL (may be it is not version 1.0, not very sure) to transform these 2 XML into 1 XML which should look like this.
OUTPUT XML:
<test>
<section>
<sect-name>Topic1\Complexity1</sect-name>
<item>
<description>The 1st Question comes here</description>
</item>
<item>
<description>The 2nd Question comes here</description>
</item>
</section>
<section>
<sect-name>Topic1\Complexity2</sect-name>
<item>
<description>The 1st Question comes here</description>
</item>
<item>
<description>The 2nd Question comes here</description>
</item>
</section>
</test>
The XSLT that I have written looks something like this, which as of now transforms a single XML into another XML and then I run a unix script to club the outputs into a single XML. I am trying to see if I can avoid using Unix.
Problem here is that every question is surrounded by a section tag. I need the section tag to be added only when the TOPIC changes, or basically the file that i am reading changes. Any help in this regard in appreciated.