Reputation: 13
I'm having an issue with my XML not showing up correctly. Basically, I have a XML document full of links and I want the XSL stylesheet to output the XML in a ordered list. Thus far, everything is working right and the styling is correct, but no data is showing for the links. You just see the stylized background. I connected XML correctly to the XSL and Dreamweaver validated the XML code without a hitch. Not sure what I'm missing here?
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="teststyle.xsl"?>
<country xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<au>
<open><li><a href="/contest/test/goto.php?id=0" target="_blank"></open>
<description>Win a Macbook!</description>
<close></a></li></close>
</au>
<au>
<open><li><a href="/contest/test/goto.php?id=1" target="_blank"></open>
<description>Win a trip to Las Vegas!</description>
<close></a></li></close>
</au>
</country>
<?xml version="1.0" encoding="utf-8"?><!-- DWXMLSource="test.xml" -->
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
<!ENTITY trade "™">
<!ENTITY mdash "—">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Untitled Document</title>
</head>
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="country/au">
<div style="background-color:teal;color:white;padding:4px">
<ol>
<span style="font-weight:bold"><xsl:value-of select="country/au/open" /><xsl:value-of select="country/au/description"/><xsl:value-of select="country/au/close"/></span>
</ol>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Views: 5470
Reputation: 2251
When you have a "for-each" block, then all the instructions inside that block are relative to the element you're running them in. That means that instead of
<xsl:value-of select="country/au/open" />
you should just use
<xsl:value-of select="open" />
Also, assuming you actually want the < and > characters from the "open" and "close" blocks, you need to disable output escaping on those links. Otherwise you'll end up with escape codes in your page.
Here is the full, working version of your XSL:
<?xml version="1.0" encoding="utf-8"?><!-- DWXMLSource="test.xml" -->
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
<!ENTITY trade "™">
<!ENTITY mdash "—">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Untitled Document</title>
</head>
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="country/au">
<div style="background-color:teal;color:white;padding:4px">
<ol>
<span style="font-weight:bold"><xsl:value-of select="open" disable-output-escaping="yes" /><xsl:value-of select="description"/><xsl:value-of select="close" disable-output-escaping="yes"/></span>
</ol>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
However, I'd strongly recommend against putting escaped html code into your xml like that. It's not very clear what's happening, and there's a lot of unnecessary mess involved in escaping all the characters. It would be better to figure out what data you actually need, and use the XSL to turn the data into valid HTML. For example, if you changed your XML data file to this:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="teststyle.xsl"?>
<country xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<au>
<url>/contest/test/goto.php?id=0</url>
<target>_blank</target>
<description>Win a Macbook!</description>
</au>
<au>
<url>/contest/test/goto.php?id=1</url>
<target>_blank</target>
<description>Win a trip to Las Vegas!</description>
</au>
</country>
then this XSL makes the behavior a bit clearer (and you don't need to handle any escaping!):
<?xml version="1.0" encoding="utf-8"?><!-- DWXMLSource="test.xml" -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
</head>
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="country/au">
<div style="background-color:teal;color:white;padding:4px">
<ol style="font-weight:bold">
<a href="{url}" target="{target}"><xsl:value-of select="description"/></a>
</ol>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Upvotes: 2