This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes XSL Table output. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XSL Table output." Watch "XSL Table output." New topic
Author

XSL Table output.

Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Hi,
Could anybody please give me tips to write a
XSL file for displaying the below XML file in
Tabular format.I'm getting all data in single line.
<root>
<data1>sdksa</data1>
<data2>asa</data2>
<data1>a</data1>
<data2>yui</data2>
<data1>asa</data1>
<data2>avc</data2>
<data1>sdfda</data1>
<data2>as67fa</data2>
</root>
Regards
Balaji


Spritle Software Blogs
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Hi Ajith,
Please answer for my question,I'm clueless now.I'm a novice in XSL.
Regards
Balaji
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Hi Mapraputa Is,Michael Ernest,Ajith ..
Please give me some reply,I'm not able to proceed further.I have no other way to go.
Regards
Balaji.

Originally posted by Balaji Loganathan:
Hi,
Could anybody please give me tips to write a
XSL file for displaying the below XML file in
Tabular format.I'm getting all data in single line.
<root>
<data1>sdksa</data1>
<data2>asa</data2>
<data1>a</data1>
<data2>yui</data2>
<data1>asa</data1>
<data2>avc</data2>
<data1>sdfda</data1>
<data2>as67fa</data2>
</root>
Regards
Balaji
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Balaji, what your tabular format is? If you convert to HTML, you can use <br/> element, so it will be like:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="root">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="node()">
<xsl:value-of select="."/><br/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
[ March 11, 2002: Message edited by: Mapraputa Is ]

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Thanks a lot Mapraputa,
I thought I won't get reply but your replyed,thank again.
I immed tried your code
type="text/xsl" href="root.xsl"
sdksa
asa
a
yui
asa
avc
sdfda
as67fa
</pre>
What i'm want to see in IE is
<table>
<tr><td>sdksa asa</td></tr>
<tr><td>a yui</td></tr>
<tr><td>asa avc</td></tr>
<tr><td>sdfda as67fa</td></tr>
</table>
Is it possible ?? How to remove type="text/xsl" href="root.xsl"
By the way where did u learned to use node(),text(),i'm using the book XSLT Programmers's reference.
Regards
Balaji
[ March 12, 2002: Message edited by: Balaji Loganathan ]
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Is it possible ??
Possible, but tricky. You need to output <tr><td> before data1 tag, and </td></tr> after data2. You could write simply:
<xsl:template match="data1">
<tr><td></xsl:text><xsl:value-of select="."/>
</xsl:template>
<xsl:template match="data2">
<xsl:value-of select="."/></td></tr>
</xsl:template>
but this will violate well-formedness, because tr and td tags are open and not properly closed within container element. We need to use a trick:
<xsl:text disable-output-escaping="yes"> &lt;td></xsl:text>
Now, from XSLT point of view, &lt;td> is just a text, so there is no well-formedness violation.
The whole stylesheet will be:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="root">
<table>
<xsl:apply-templates/>
</table>
</xsl:template>
<xsl:template match="data1">
<xsl:text disable-output-escaping="yes">&lt;tr>&lt;td></xsl:text><xsl:value-of select="."/><xsl:text> </xsl:text>
</xsl:template>
<xsl:template match="data2">
<xsl:value-of select="."/><xsl:text disable-output-escaping="yes">&lt;/td>&lt;/tr></xsl:text>
</xsl:template>
</xsl:stylesheet>
Doesn't look too elegant to me, though. Maybe there is an easier solution
How to remove type="text/xsl" href="root.xsl"
This construction is used if you rely on IE to perform transformation. If you use it to learn XSLT, it's Ok. Otherwise, you should use XSLT processor on server side, such as Xalan or Saxon.
By the way where did u learned to use node(),text(),i'm using the book XSLT Programmers's reference.
Michal Kay's? it's a very good book, but it's a reference, which means it wasn't designed to teach XSLT. I used Khun Yee Fung's "XSLT", it is very beginner-friendly. But there are many other now, most of them will work, if they are not references
[ March 12, 2002: Message edited by: Mapraputa Is ]
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Thanks and Thanks a LOTS...
First I thought its not possible,but you made it, you are really great.
Well I'm going to sincerely read the Michael Kay book.
Thanks again.
Regards
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
There is even better possible solution. The last one depended on tag names, which is not necessarily and not advisable. Instead, you can check position of each element and use mod() function to differentiate odd and even elements.
<xsl:template match="root">
<table>
<xsl:for-each select="./*">
<xsl:choose>
<xsl:when test='position() mod 2 = 1'>
<xsl:text disable-output-escaping="yes">&lt;tr>&lt;td></xsl:text>
<xsl:value-of select="."/><xsl:text> </xsl:text>
</xsl:when>
<xsltherwise>
<xsl:value-of select="."/><xsl:text disable-output-escaping="yes">&lt;/td>&lt;/tr></xsl:text>
</xsltherwise>
</xsl:choose>
</xsl:for-each>
</table>
</xsl:template>
This solution will break if your XML doesn't have flat structure, but in this case you probably do not want to output it as a table anyway...
[ March 12, 2002: Message edited by: Mapraputa Is ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XSL Table output.
 
Similar Threads
xsl:if and XPath
Displaying ResultSets in View
Passing data to JSP
little confusion with appendChild()
Reading a complex file