Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic html table in xslt

Status
Not open for further replies.

NuJoizey

MIS
Aug 16, 2006
450
US
I'm sure this problem has been worked out many times before, but my experience with XSLT apparently isn't good enough to allow me to see how to do the following:

say the XML looks something like this:
Code:
<events>
	<event>
		<name>concert</name>
		<date>today</date>
		<time></time>
		<location>New Jersey</location>
	</event>
	<event>
		<name>book signing</name>
		<date></date>
		<time></time>
		<location>New York</location>
	</event>
</events>

I need my XSLT to render a table that looks like:
Code:
<table>
[COLOR=red](this part is easy.  I know the XML will always render two values here)[/color]

	<tr>
		<th>concert</th>
		<th>book signing</th>
	</tr>

[COLOR=red](here's where it gets tricky and where I lose grasp of how to get what I want. - not all elements have values, and  it needs to be dynamic - I want, in the case of this example, the book signing location to render at the cell in the top right of the table instead of bottom right, and the &nsbp' s to follow at the end)[/color]

	<tr>
		<td>date: today</td>
		<td>location:  New York</td>
	</tr>
	<tr>
		<td>location: New Jersey</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	<tr>
</table>

Can this be done in pure XSLT? Should it be done in XSLT, or do you need server side code?

to manipulated using procedural rather than declarative methods? Does anyone know of a pattern or a similar example I can reference to help me get what I need?

The following html rendering is NOT acceptable: (i got this far to work in my XSLT, but it's not what is called for in the spec)
Code:
<table>
	<tr>
		<th>concert</th>
		<th>book signing</th>
	</tr>
	<tr>
		<td>date: today</td>
		<td>&nbsp;</td>
	</tr>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td>location: New Jersey</td>
		<td>location: New York</td>
	<tr>
</table>

Uncommitted, except to looking good. Constantly judging.
Always on the verge of being upset.
 
[tt]<!-- suppose in the context of child nodes of "event" -->
<xsl:choose>
<xsl:when test="normalize-space()=''">
<xsl:value-of select="'&nbsp;'" />
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="normalize-space()" />
</xsl:eek:therwise>
</xsl:choose>
[/tt]
The contingent bit of "date: " or "location: ", you have added that per node's tag name using concate() to add that prefix that you've to expand the above yourself.
 
tsuji,

Thanks for trying to help me.

I'm not sure how the normalize-space function would help me here. I think I was doing something similar testing using test=event[not(.='')]

Am I missing something? I think that xml:choose block would still render a result that looks like this:


=============================
concert book signing
=============================
today

new jersey new york
============================


when what I need is this:


=============================
concert book signing
=============================
today new york
new jersey
=============================



Uncommitted, except to looking good. Constantly judging.
Always on the verge of being upset.
 
normalize-space() used in this context will test false if the text value of the node contains nothing, or nothing but whitespace. This is a normal practice -- not meant to obscure.

I must admit that I am having difficulty understanding the relationship of the various elements to the desired output. Is it that you want any content in the upper <td> elements in a column, and fill lower <td> elements with [tt]nbsp[/tt]?

Tom Morrison
Micro Focus
 
Is it that you want any content in the upper <td> elements in a column, and fill lower <td> elements with nbsp?

yes that is precisely it. I thought I was being clear. Sorry. Note the difference in my desired vs non desired output, and it is only that it has to do with spacing. If the "book signing" event only has location information, and not date and time, then I want it to be in the top right of the table.

Similarly, if the concert has only date and location information, but not time, I want the location information to move up into the spot that would ordinarily be occupied by the time information, if there were any.

If an event contains date, time and location information, then all three should be populated.

Is this more clear?

Uncommitted, except to looking good. Constantly judging.
Always on the verge of being upset.
 
I should be more precise on the &nbsp; and the context nodes. They are off. Here is the proper script.
[tt]
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="events" />
</body>
</html>
</xsl:template>
<xsl:template match="events">
<table border="1">
<tr>
<th>concert</th>
<th>book signing</th>
</tr>
<xsl:apply-templates select="event" />
</table>
</xsl:template>
<xsl:template match="event">
<tr>
<xsl:apply-templates select="date|location" />
</tr>
</xsl:template>
<xsl:template match="date|location">
<td>
<xsl:choose>
<xsl:when test="normalize-space()=''">
<xsl:value-of select="'&#xa0;'" />
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="concat(name(),': ', normalize-space())" />
</xsl:eek:therwise>
</xsl:choose>
</td>
</xsl:template>
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top