473,320 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Tricky XSL question (?)

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
Jul 20 '05 #1
4 1857
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

Jul 20 '05 #2
"Janwillem Borleffs" <jw@jwscripts.com> wrote in news:3ff5a8d5$0$16882
$1*******@news.wanadoo.nl:
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
Jul 20 '05 #3
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

Jul 20 '05 #4
"Janwillem Borleffs" <jw@jwscripts.com> wrote in
news:3f***********************@news.wanadoo.nl:
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
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Lars Plessmann | last post by:
Problem: I try to store data in a objects field and read it out again. Sounds easy, yeah. But its a bit tricky here.... ;-) This is the class Customer.php with some setter and getter functions...
0
by: dracolytch | last post by:
Good day all, Ok, I have a pretty tricky problem that I need some help with. I pass around search query information a fair amount (specifically WHERE statements). Normally, I just rawurlencode()...
1
by: JZ | last post by:
Oracle 9iR2 I have a table: SQL> select * from test; A B C ------------------- ---------- ---------- 01/01/2004 10:00:00 1 1...
4
by: Bung | last post by:
Hi, I have a tricky sql statment I have to write (tricky for me) and I am stuck. I'm having trouble with the following problem. Table1 (Column a, Column b, Column c) Table2 (Column a, Column...
4
by: Angel Cat | last post by:
I have 2 tables joined together by the IDs, People and the pets they own PEOPLE ID NAME 1 JohnSMith 2 JaneDoe PETS ID PET
25
by: PyPK | last post by:
What possible tricky areas/questions could be asked in Python based Technical Interviews?
5
by: Danny | last post by:
Hi there I need help with a tricky problem. I have a 2 dimensional array with qualities such as ball size, ball color, ball weight. Now I have to print out all the possible combinations of...
8
by: pras.vaidya | last post by:
Hi , below given question was asked to me during an interview and i figured it out little tricky . It would be a great help if anyone could solve it. Code : - main() { char...
7
by: NileshKorpe | last post by:
Can you please send me link of some c++ tricky (confusing) questions usually asked in the c++ technical interview. Thank You
1
by: MorrganMail | last post by:
Or at least I find it tricky. :-) Assume we have three tables A, B and C. Table A contains a path and the distance for traveling that path: A (PathId, NodeId, Dist (from previous node)) 1, 1,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.