Forum Moderators: open

Message Too Old, No Replies

On to Variables

The Adventures of Newton the n00b Continues...

         

cmarshall

11:11 am on Mar 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Moving right along, I've gotten successful XML transformation from my raw XML dump to valid XHTML 1.1. Everything validates well, and the transformation is very literal. That is, it displays everything in place.

I should mention that my XML "hello world" program is actually a dump of a schedule system. A MySQL table is being dumped. Each row contains a couple of dozen data items, such as name of event, name of location, time, day of week, address, URI for more info, etc. The dump can be several thousand lines long. The XSLT/XML/Schema handles this no problem.

Today's issue is how to use variables. I don't just want things dumped in place. I want to take a URI, and wrap a name in an anchor element, with the href being that URI.

These are two separate data points, so I want to store each one, and combine them after the fact.

Here is an example of the XML dump (this is a teeny example, not my main program):

<?xml version="1.0" encoding="ISO-8859-1"?>
<weetest xmlns="http://www.example.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.com file:/Users/cmarshall/Desktop/XMLTest/wee_test/wee_test.xsd">
<elementwrapper id="1">
<data_name>Test</data_name>

</elementwrapper>
</weetest>

What I want is output that looks like this:

<!DOCTYPE html
PUBLIC "-//W3c//DTD HTML 4.01//EN" "http://www.w3c.org/tr/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>XML Test</title>
</head>
<body>
<ul>
<li>
<ul>
<li><a href="http://www.example.com/wee_test.html">Test</a></li>
</ul>
</li>
</ul>
</body>
</html>

So far, the book I'm using is so completely worthless that it is impossible to describe the level of uselessness without using quantum theory. I'm on my own.

I know it can be done, and I'll figure it out, but it may take a bit.

cmarshall

5:58 pm on Mar 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, we're narrowing it down. It has to do with the namespace of the XML file.

Here's the current XML file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<weetest xmlns="http://www.example.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.com file:/Users/cmarshall/Desktop/XMLTest/wee_test/wee_test.xsd"
>
<elementwrapper id="1">
<data_name>Test</data_name>

</elementwrapper>
</weetest>

The red stuff is what I'd have to remove to make it work.

And here's the current XSLT file:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wt="http://www.example.com" exclude-result-prefixes="wt">
<xsl:output method="html" version="4.01"/>
<xsl:output doctype-system="http://www.w3c.org/tr/html4/strict.dtd"/>
<xsl:output doctype-public="-//W3c//DTD HTML 4.01//EN"/>
<xsl:template match="/">
<html>
<head>
<title>XML Test</title>
</head>
<body>
<xsl:for-each select="weetest/elementwrapper">
<div>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of disable-output-escaping="no" select="data_uri"/>
</xsl:attribute>
<xsl:value-of select="data_name"/>
</xsl:element>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Now, if I remove the default namespace from the XML file, thusly:

<?xml version="1.0" encoding="ISO-8859-1"?>
<weetest>
<elementwrapper id="1">
<data_name>Test</data_name>

</elementwrapper>
</weetest>

it works, but I can't do that. I need the namespace, so that will be the next challenge.

cmarshall

6:21 pm on Mar 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, I've solved it.

You need to specify the namespace prefix on every part of the

select
path, like so:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wt="http://www.example.com" exclude-result-prefixes="wt">
<xsl:output method="html" version="4.01"/>
<xsl:output doctype-system="http://www.w3c.org/tr/html4/strict.dtd"/>
<xsl:output doctype-public="-//W3c//DTD HTML 4.01//EN"/>
<xsl:template match="/">
<html>
<head>
<title>XML Test</title>
</head>
<body>
<xsl:for-each select="wt:weetest/wt:elementwrapper">
<div>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of disable-output-escaping="yes" select="wt:data_uri"/>
</xsl:attribute>
<xsl:value-of select="wt:data_name"/>
</xsl:element>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

This gives us:

<!DOCTYPE html PUBLIC "-//W3c//DTD HTML 4.01//EN" "http://www.w3c.org/tr/html4/strict.dtd">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>XML Test</title>
</head>
<body>
<div>
<a href="http://www.example.com/wee_test.html">Test</a>
</div>
</body>
</html>

[edit reason]Fixed a typo[/edit reason]