Page 1 of 1

XSLT iterating over dynamically generated XML.

Posted: Wed Feb 07, 2007 7:25 am
by Amnesiac
Hi all,

I am trying to iterate over an xml document that does not have a defnite structure, it is generated dynamically and the name of the nodes are not at the same level always. I need to generate a collapsable and expandable structure at every level of the nodes.

I request you to please throw some light on this. I have been trying to do things but its not working out. If I am able to display just the immediate child nodes(not all child nodes) of a particular node I may be able to get some headway.

I have tried google but it hasn't helped. I am not really used to asking these type of questions(silly if it may sound), please help me.

Thanks a lot,
Amnesiac.

Posted: Wed Feb 07, 2007 10:57 am
by sorin_ristache
Hello,

Your problem is too general. Without some sample files of the input XML and the expected result I think there are not many chances of receiving help. What did you try already, what is the current form of your stylesheet and what does not work in your stylesheet?


Regards,
Sorin

Posted: Wed Feb 07, 2007 2:00 pm
by Amnesiac
Thank you for the reply.

Sample XML FIle:

Code: Select all



<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="Trial.xsl"?>
<Log>
<Request>frequest()<fRequest>
<data>1</data>
<data>2</data>
<data>3</data>
</fRequest>
</Request>

<ARequest>aReq
</ARequest>

<BRequest>bReq
</BRequest>
</Log>
I want to display this xml file as a expandable and collapsable structure in an HTML page. I am using DIV tags for expanding and collapsing. The xml file could have anything except for the first root element 'Log'.

This is my XSL file:

Code: Select all



<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Log">
<html>
<body>
<h2> Log Details </h2>
<table border="1">
<tr><th>Node-set></th> <th>Value</th></tr>

<script language="JavaScript">
var idGen =0;
var evenTrack =0;
function expandIt(whichEl) {
whichEl.style.display = (whichEl.style.display == "none" ) ? "" : "none";
}
function generateNextID(){
return idGen++;
}
function getGenID(){
return idGen;
}
</script>
<xsl:call-template name="showChildren" />
</table>

</body>
</html>
</xsl:template>

<xsl:template name="showChildren" >
<xsl:param name="mat" select="Log"/>
<a href="javascript:expandIt(generateNextID())" ><xsl:value-of select="name(.)" /></a>
<div id="javascript:getGenID()" style="display:all">
<table border="1">
<xsl:for-each select="child::node()">
<tr>
<td> <xsl:call-template name="showChildren" /> </td>
<td><xsl:value-of select="text()"/></td>
</tr>
</xsl:for-each>
</table>
</div>

</xsl:template>

</xsl:stylesheet>
The problem is when I click on the Href it tries to access a DIV tag which is not there. I am trying a way to eliminate the JS function call from Href and place in some static thing there. Basically I am trying to tie a particular href to a particular DIV tag. If you copy these files and double click on the .xml file you will understand.

Thanks a lot for the reply,
Amesiac.

Posted: Thu Feb 08, 2007 10:56 am
by Amnesiac
I solved it, I used the generate-id XSLT function to generate a unique id for every node and assigned the same id to the coresponding div tags.

Thanks,
Vinay.