Tricky XSL question (?)

J

Jyrki Keisala

I have an XML file:

<document>

<record>
<element1>Text</element1>
<element2>More text</element2>
<element3>Even more text</element3>
<element4>Some text <link><title>Google</title><url>
http://www.google.com</url></link> with a link.</element4>
</record>

<record>
<element1>Yeah</element1>
<element2>Now the link is here: <link><title>W3C</title><url>
http://www.w3c.org</url></link></element2>
<element3>Something else</element3>
<element4>...and more text</element4>
</record>

</document>

which I want to convert to an HTML table. One possibility for the
conversion is to use for-each loop in XSL as in

(XSL1)

<xsl:template match="/">
<html>
<head>
<title>My HTML table</title>
</head>
<body>
<table border="1">
<head>
<tr>
<td><b>Element1</b></td>
<td><b>Element2</b></td>
<td><b>Element3</b></td>
<td><b>Element4</b></td>
</tr>
</head>
<xsl:for-each select="document/record">
<tr>
<td><xsl:value-of select="element1" /></td>
<td><xsl:value-of select="element2" /></td>
<td><xsl:value-of select="element3" /></td>
<td><xsl:value-of select="element4" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

This will produce the table nicely, but what about the

<link>
<title>Title of the URL link</title>
<url>http://some.url.link</url>
</link>

structure I have in my XML file? I would like to be able to put such a
link structure inside any element I have in the XML record. The link
structure itself is easily enough translated into a URL with an XSL
transformation of

(XSL2)

<xsl:template match="//link">
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="url" />
</xsl:attribute>
<xsl:value-of select="title" />
</xsl:element>
</xsl:template>

But when I want to have the possibility of having URL links anywhere in
my HTML table, straight combining of (XSL1) and (XSL2) templates
fails...seems to have something to do with using the value-of structure
in (XSL1)...?

Regards,
Jyrki Keisala
 
J

Janwillem Borleffs

Jyrki said:
This will produce the table nicely, but what about the

<link>
<title>Title of the URL link</title>
<url>http://some.url.link</url>
</link>

structure I have in my XML file? I would like to be able to put such
a link structure inside any element I have in the XML record.

This ought to do it:

<xsl:template match="/">
<html>
<head>
<title>My HTML table</title>
</head>
<body>
<table border="1">
<head>
<tr>
<td><b>Element1</b></td>
<td><b>Element2</b></td>
<td><b>Element3</b></td>
<td><b>Element4</b></td>
</tr>
</head>
<xsl:for-each select="document/record">
<tr>
<xsl:for-each select="child::*">
<td><xsl:apply-templates select="link|text()" /></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="link">
<a href="{url}"><xsl:value-of select="title" /></a>
</xsl:template>


HTH,
JW
 
J

Jyrki Keisala

This ought to do it:

<xsl:template match="/">
<html>
<head>
<title>My HTML table</title>
</head>
<body>
<table border="1">
<head>
<tr>
<td><b>Element1</b></td>
<td><b>Element2</b></td>
<td><b>Element3</b></td>
<td><b>Element4</b></td>
</tr>
</head>
<xsl:for-each select="document/record">
<tr>
<xsl:for-each select="child::*">
<td><xsl:apply-templates select="link|text()" /></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="link">
<a href="{url}"><xsl:value-of select="title" /></a>
</xsl:template>


Thanks for the valuable tip. Everything seems to work just fine; I have
just one further question: for what is the "select="link|text()" needed?
Everything seems to work just fine for me even without that, so a plain

<xsl:for-each select="child::*">
<td><xsl:apply-templates /></td>
</xsl:for-each>

seems to be able to handle the links exactly correct...

Regards,
Jyrki
 
J

Janwillem Borleffs

Jyrki said:
Thanks for the valuable tip. Everything seems to work just fine; I
have just one further question: for what is the "select="link|text()"
needed? Everything seems to work just fine for me even without that,
so a plain

<xsl:for-each select="child::*">
<td><xsl:apply-templates /></td>
</xsl:for-each>

seems to be able to handle the links exactly correct...

It selects the <link /> element or a text node when available. I have put it
in mainly to show you how to work with apply-templates select attribute.

This can be very useful when you want to match specific nodes only...


JW
 
J

Jyrki Keisala

It selects the <link /> element or a text node when available. I have
put it in mainly to show you how to work with apply-templates select
attribute.

This can be very useful when you want to match specific nodes only...


JW

OK, thanks veeery much; it works for me now :)

Jyrki
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top