Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Newbie question frustrated on basic XSL

4 views
Skip to first unread message

terminate

unread,
Dec 16, 2009, 12:16:41 PM12/16/09
to
I'm having trouble. I've been learning XSL for about a week now and I
can't figure this out. Why is this printing the URL's at the bottom.
I'm using XSL to select countries and to print their respective
medals, but it's still printing some of the URLS in the xml for some
odd reason.

XSL --------------------------------------------------------------
--------------------------------------------------------------

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">
<xsl:output method="html"/>

<xsl:template match="/">

<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="MedalsSummary/country[@noc='CAN']">
<p><font color="red"><xsl:value-of select="@totalgold"/><br/></
font></p>
<p><font color="red"><xsl:value-of select="@totalsilver"/><br/></
font></p>
<p><font color="red"><xsl:value-of select="@totalbronze"/><br/></
font></p>
</xsl:template>


XML: --------------------------------------------------------------
--------------------------------------------------------------

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="medals.xsl"?>

<MedalsSummary imgSrc="http://www.ctvolympics.ca/imgml/icons/
medals_100x100.jpg">

<country noc="NOR" FullName="Norway" totalgold="98"
totalsilver="98" totalbronze="84" totalmedals="280" totalorder=""
CountryLink="http://www.ctvolympics.ca/countries/country=nor/
index.html"
flagURL="http://www.ctvolympics.ca/imgml/flags/100x100/NOR.jpg" />

<MedalStandNews>http://www.ctvolympics.ca/news-centre/index.html</
MedalStandNews>
<MedalStandPhotos>http://www.ctvolympics.ca/photos/index.html</
MedalStandPhotos>
<MedalStandVideo>http://www.ctvolympics.ca/video/index.html</
MedalStandVideo>
<MedalCount>http://www.ctvolympics.ca/medals/index.html</MedalCount>
<Medalists>http://www.ctvolympics.ca/medals/medalists/index.html</
Medalists>

</MedalsSummary>

</xsl:stylesheet>

Martin Honnen

unread,
Dec 16, 2009, 1:32:27 PM12/16/09
to
terminate wrote:
> I'm having trouble. I've been learning XSL for about a week now and I
> can't figure this out. Why is this printing the URL's at the bottom.
> I'm using XSL to select countries and to print their respective
> medals, but it's still printing some of the URLS in the xml for some
> odd reason.

There are built-in templates, see
http://www.w3.org/TR/xslt#built-in-rule
These help in processing level by level without explicitly writing
templates.

See below for further explanation:

> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
> Transform">
> <xsl:output method="html"/>
>
> <xsl:template match="/">
>
> <html>
> <body>
> <xsl:apply-templates/>

So here, in the template matching the document node (/) you do
xsl:apply-templates without a select attribute which is the same as
doing apply-templates select="node()" to select all child nodes (of the
document node) for further processing.
Without the built-in templates your stylesheet would stop here as the
child node of the document node is the root element named MedalsSummary
for which you haven't written any template.
However due to the built-in templates the MedalsSummary element is
processed by
<xsl:template match="*">
<xsl:apply-templates/>
</xsl:template>
which does not output anything to the result tree but selects all child
nodes of the MedalsSummary element for further processing.
Some of those child nodes are those MedalStandNews, MedalStandPhotos,
and so on elements for which you haven't written a template so again a
built-in template is used which again does not output anything but
processes all child nodes.
As those element have text node children and the built-in template for
that is
<xsl:template match="text()|">
<xsl:value-of select="."/>
</xsl:template>
which outputs the text node you get output you don't want.

One cure is, where you do apply-templates without a select, do instead
simply select the element(s) you are interested in e.g.
<xsl:apply-templates select="MedalsSummary/country[@noc='CAN']"/>
Note however that your input XML below has no element with @noc='CAN' so
I am not sure what exactly you want to achieve.
But with that select in the template with match="/" you would at least
not have the built-in templates process elements you are not interested in.

Sometimes you want to exploit that built-in templates ensure that
processing continues but don't want all text nodes to be output, that
can be achieved by overriding the built-in template with e.g.
<xsl:template match="text()"/>


--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/

terminate

unread,
Dec 17, 2009, 4:18:48 PM12/17/09
to

Hi Martin,

Thank you very much for the clarification. I appreciate it.

Andrew

0 new messages