Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Tricky XSL question (?)

Reply
Thread Tools

Tricky XSL question (?)

 
 
Jyrki Keisala
Guest
Posts: n/a
 
      01-02-2004
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
 
Reply With Quote
 
 
 
 
Janwillem Borleffs
Guest
Posts: n/a
 
      01-02-2004
Jyrki Keisala wrote:
> 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



 
Reply With Quote
 
 
 
 
Jyrki Keisala
Guest
Posts: n/a
 
      01-03-2004
"Janwillem Borleffs" <(E-Mail Removed)> wrote in news:3ff5a8d5$0$16882
$(E-Mail Removed):

> Jyrki Keisala wrote:
>> 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>



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
 
Reply With Quote
 
Janwillem Borleffs
Guest
Posts: n/a
 
      01-03-2004
Jyrki Keisala wrote:
> 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



 
Reply With Quote
 
Jyrki Keisala
Guest
Posts: n/a
 
      01-05-2004
"Janwillem Borleffs" <(E-Mail Removed)> wrote in
news:3ff6c163$0$23710$(E-Mail Removed):

> Jyrki Keisala wrote:
>> 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
>
>
>
>


OK, thanks veeery much; it works for me now

Jyrki
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
tricky xsl transformation needed... stefan.oedenkoven@googlemail.com XML 2 06-16-2008 02:21 PM
Tricky XSL transformation jl XML 3 08-23-2006 01:57 PM
a tricky if else(maybe not tricky but impossible) nirkheys@gmail.com C Programming 9 04-25-2006 06:13 PM
XSL Question tp xsl:for-each and xsl:variable schaf@2wire.ch XML 1 05-27-2005 09:25 PM
Tricky XSL xinclude question rene XML 3 05-02-2005 08:25 AM



Advertisments