Rikotech
Rikotech

Reputation: 73

Why the value of the tag isn't outputted

I am new in XSLT and I don't understand the basic idea of templates. I am doing an experiment to see if the output is what I expect. Unfortunately not. This are the files in transformation:

The XML:

<?xml version="1.0" encoding="UTF-8"?>

<catalog>
    <cd>
        <title>Empire</title>
        <artist>Bob Dylan</artist>
        <continent>America</continent>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>Bulgaria</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
</catalog>

And this is the XSLT:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text"/>
    <xsl:template match="/">
        <xsl:apply-templates select="//continent"/>
    </xsl:template>
    <xsl:template match="//continent">
        <xsl:value-of select="continent"/>
    </xsl:template>
</xsl:stylesheet>

The output is empty sheet. I expect the value of the continent tag to be printed, namely America. Please shed some light on this matter. Thank tou.

Upvotes: 0

Views: 17

Answers (1)

potame
potame

Reputation: 7905

The error lies here:

<xsl:template match="//continent">
    <xsl:value-of select="continent"/>
</xsl:template>

In this template, you have matched a <continent>, and with <xsl:value-of select="continent" /> you try to retrieve information from another <continent> child tag (which do not exist).

If you use <xsl:value-of select="."/>, <xsl:value-of select="text()"/> or even <xsl:apply-templates />, you should obtain the desired output.

Upvotes: 1

Related Questions