Creating links with xsl from xml

S

schilde

Hello,

I want to transform a xml to html with xsl which is not this big
problem. But when I want to create a link with informations of the xml
it doesn't work. To illustrate the problem here an example:

link_example.xml

<booklist>
<book>
<id>1</id>
<title>blabla</title>
</book>
<book>
<id>2</id>
<title>blblblblbla</title>
</book>
</booklist>

The output should look like this. There are a list of links which
reference down to the titles in the same document:


Booklist: 1(as a link) 2(as link)

1 blabla
2 blblblblbla


<a href="{id}"><value-of select=".">..... didn't work. I don't know what
to do. Could someone help?

Greetings

Markus
 
M

Martin Honnen

schilde wrote:

I want to transform a xml to html with xsl which is not this big
problem. But when I want to create a link with informations of the xml
it doesn't work. To illustrate the problem here an example:

link_example.xml

<booklist>
<book>
<id>1</id>
<title>blabla</title>
</book>
<book>
<id>2</id>
<title>blblblblbla</title>
</book>
</booklist>

The output should look like this. There are a list of links which
reference down to the titles in the same document:


Booklist: 1(as a link) 2(as link)

1 blabla
2 blblblblbla

If I understand you correctly you want to process the <book> elements
twice, once to generate the list with links, once to generate the
detailed list with book titles. One way to do that is template with
modes so that an element can be processed in different modes, here is an
example:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:eek:utput method="html" encoding="utf-8" indent="yes" />

<xsl:template match="/">
<html>
<head>
<title>A list of books</title>
</head>
<body>
<div>
<p>Booklist:
<xsl:apply-templates select="booklist/book" mode="link" />
</p>
<div>
<ol>
<xsl:apply-templates select="booklist/book" />
</ol>
</div>
</div>
</body>
</html>
</xsl:template>

<xsl:template match="book">
<li id="b{position()}"><xsl:value-of select="title" /></li>
</xsl:template>

<xsl:template match="book" mode="link">
<a href="#b{position()}"><xsl:value-of select="position()" /></a>
<xsl:text> </xsl:text>
</xsl:template>

</xsl:stylesheet>

The resulting HTML is alike

<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>A list of books</title>
</head>
<body>
<div>
<p>Booklist:
<a href="#b1">1</a>&nbsp;<a href="#b2">2</a>&nbsp;<a
href="#b3">3</a>&nbsp;<a href="#b4">4</a>&nbsp;</p>
<div>
<ol>
<li id="b1">blabla</li>
<li id="b2">blblblblbla</li>
<li id="b3">Kibology for all</li>
<li id="b4">All for Kibology</li>
</ol>
</div>
</div>
</body>
</html>
 

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

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top