XML/XSL Help

H

HugeBob

Hello All,

I'm experimenting with XML and I'm working on an example I've made up.
Basically, this is the format:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Baseball.xsl"?>

<MLB>
<team name="Yankees">
<player last="Jeter" first="Derek"/>
<player last="Abreu" first="Bobby"/>
<player last="Giambi" first="Jason"/>
<player last="Williams" first="Bernie"/>
<player last="Cano" first="Robinson"/>
<player last="Johnson" first="Randy"/>
</team>
<team name="Red Sox">
<player last="Ramierez" first="Manny"/>
<player last="Ortiz" first="David"/>
<player last="Schilling" first="Curt"/>
<player last="Becket" first="Josh"/>
<player last="Varitek" first="Jason"/>
.
.
.
</MLB>

Basically, I want to traverse the tree starting from MLB and kick out
each team name. I've tried this XSL. But, it's not working:

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">

<xsl:template match="/MLB">
<HTML>
<HEAD>
<TITLE>
MLB Teams
</TITLE>
</HEAD>
<body bgcolor="#FFFFDD">
<xsl:for-each select="team/@name">
<xsl:value-of select="text()"/><BR />
</xsl:for-each>
</body>
</HTML>
</xsl:template>
</xsl:stylesheet>

Any direction will be appreciated. Thanks.
 
M

Martin Honnen

HugeBob wrote:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
^^^^^^^^^^^^^^^^^^^^^^^^^^^
The correct namespace URI for XSLT 1.0 and 2.0 is
http://www.w3.org/1999/XSL/Transform and not what you have (which is a
URI used in an outdated working draft before XSLT 1.0 became a
specification in 1999).

<xsl:template match="/MLB">
<xsl:for-each select="team/@name">
<xsl:value-of select="text()"/><BR />

If you are iterating over attributes and what to output the value then
you need
<xsl:value-of select="."/>
 
J

Joe Kesselman

You need a root template so XSLT knows where to start. Add:

<xsl:template match="/">
<xsl:apply-templates select="/MLB"/>
</xsl:template>

(Or change your match="/MLB" template to match="/" and adjust its
contents -- specifically, the for-each -- appropriately.)
 
H

HugeBob

That worked! Thanks Martin.

Martin said:
^^^^^^^^^^^^^^^^^^^^^^^^^^^
The correct namespace URI for XSLT 1.0 and 2.0 is
http://www.w3.org/1999/XSL/Transform and not what you have (which is a
URI used in an outdated working draft before XSLT 1.0 became a
specification in 1999).




If you are iterating over attributes and what to output the value then
you need
<xsl:value-of select="."/>
 
M

Martin Honnen

Joe said:
You need a root template so XSLT knows where to start. Add:

<xsl:template match="/">
<xsl:apply-templates select="/MLB"/>
</xsl:template>

Why would he need that? The built-in template for the root node / takes
care of that:
<http://www.w3.org/TR/xslt#built-in-rule>

<xsl:template match="*|/">
<xsl:apply-templates/>
</xsl:template>
 

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,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top