How to Display XML tags and values in an HTML page?

D

drawbridgej

I've done a little xml and xsl, but am a relative newbie.
I have been unable to find a stylesheet to process an xml file and
output the tagNames and tagValues in HTML. I have also posted this
message in tek-tips.

I can get the node names with name() and the value with xsl:value-of
.... but I would like to get the lowest level tagNames via the xsl. I'd
like to be able to build html for a number of different xml files.


I'm trying to get something like:

Flights
Flight 1
Flight_Number: BA123
Origin : GLA
Destination : LHR
Carrier : British Airways
Date : 01/01/2002
.....
other flights

from xml as follows:
<Flights>
<Flight>
<Flight_Number>BA123</Flight_Number>
<Origin>GLA</Origin>
<Destination>LHR</Destination>
<Carrier>British Airways</Carrier>
<Date>01/01/2002</Date>
</Flight>
<Flight>
<Flight_Number>BA4234</Flight_Number>
<Origin>GLA</Origin>
<Destination>YOW</Destination>
<Carrier>British Airways</Carrier>
<Date>01/01/2002</Date>
</Flight>
</Flights>


I've tried name() and local-name() but they return the name of the
parent node, not the leaf element name.
TIA

jack
 
P

Peter Flynn

I've done a little xml and xsl, but am a relative newbie.
I have been unable to find a stylesheet to process an xml file and
output the tagNames and tagValues in HTML. I have also posted this
message in tek-tips.

First of all, please read http://xml.silmaril.ie/authors/makeup/
I can get the node names with name() and the value with xsl:value-of
... but I would like to get the lowest level tagNames via the xsl. I'd
like to be able to build html for a number of different xml files.

Have you set said:
I'm trying to get something like:

Flights
Flight 1
Flight_Number: BA123
Origin : GLA
Destination : LHR
Carrier : British Airways
Date : 01/01/2002
.....
other flights

from xml as follows:
<Flights>
<Flight>
<Flight_Number>BA123</Flight_Number>
<Origin>GLA</Origin>
<Destination>LHR</Destination>
<Carrier>British Airways</Carrier>
<Date>01/01/2002</Date>
</Flight>
<Flight>
<Flight_Number>BA4234</Flight_Number>
<Origin>GLA</Origin>
<Destination>YOW</Destination>
<Carrier>British Airways</Carrier>
<Date>01/01/2002</Date>
</Flight>
</Flights>


I've tried name() and local-name() but they return the name of the
parent node, not the leaf element name.

As you didn't post your XSL code we can only guess at what you're doing.

What you need is a template for each element of your XML document,
giving the HTML element type that you want to be output for it, eg

<xsl:template match="Flights">
<h1>
<xsl:apply-templates/>
</h1>
</xsl:template>

<xsl:template match="Flight">
<h2>
<xsl:text>Flight </xsl:text>
<xsl:number/>
</h2>
<table>
<xsl:for-each select="*">
<tr>
<td>
<xsl:value-of select="name()"/>
</td>
<td>:</td>
<td>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>

The XSL List is a good place to discuss the details of XSL[T]:
see the details of other FAQs at the address below.

///Peter
 
D

drawbridgej

Thanks Peter,

I have partially solved the issue. Here is the output and the xsl Used.

Output:

List of matching Flights

Flight
Flight_Number: BA123
Origin: GLA
Destination: LHR
Carrier: British Airways
Date: 01/01/2002

Flight
Flight_Number: BA4234
Origin: GLA
Destination: YOW
Carrier: British Airways
Date: 01/01/2002

Flight
Flight_Number: AA4959
Origin: GLA
Destination: LHR
Carrier: American Airways
Date: 01/01/2002

Flight
Flight_Number: AC23
Origin: CAN
Destination: LHR
Carrier: Air Canada
Date: 01/02/2004


xsl:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="html" indent="no"/>
<xsl:template match="Flights">
<html>
<head>
<title>Matching Request Flights</title>
</head>
<body>
<h1>List of matching Flights </h1>
<xsl:apply-templates select="Flight"/>
</body>
</html>
</xsl:template>

<!-- Templates -->
<xsl:template match="Flight">
<!-- Display Flight-->
<tr><th><br/>
<b><font color="blue"><xsl:value-of select="local-name()"/></font></b>
</th></tr>
<table border="0">
<xsl:apply-templates />
</table>
</xsl:template>

<xsl:template match="Flight/*">
<tr><td><b><xsl:value-of select="local-name()"/>:</b></td>
<td><xsl:value-of select="."/></td></tr>
</xsl:template>

</xsl:stylesheet>
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top