Forum Moderators: open
Given the following 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>
<elementwrapper id="2">
<data_name>Test</data_name>

</elementwrapper>
<elementwrapper id="3">
<data_name>Test</data_name>
</elementwrapper>
</weetest>
I have two iterations that don't have valid URIs. One has an empty
 element, and the other has none. I have modified the schema to accommodate it: <?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.com"
elementFormDefault="qualified">
<xs:element name="weetest">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="elementwrapper">
<xs:complexType>
<xs:sequence>
<xs:element name="data_name" type="xs:string"/>
<xs:element minOccurs="0" name="data_uri" type="xs:anyURI"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
And here is our 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="wt:weetest/wt:elementwrapper">
<div>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of disable-output-escaping="no" 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>
I need to tweak the stylesheet so that the name gets displayed without an anchor element if the URI is not valid.
I don't think this will be a big deal. I know there's an XPath element for conditionals.
<!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>
<div>
<a href="">Test</a>
</div>
<div>
<a href="">Test</a>
</div>
</body>
</html>
What I want is:
<!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>
<div>Test</div>
<div>Test</div>
</body>
</html>
http://www.w3schools.com/xsl/xsl_if.aspSorry to break monolog hope this is useful
Yes, it was, but I needed an alternative path, so I chose
[w3schools.com], <xsl:choose>
[w3schools.com] and <xsl:when>
[w3schools.com] instead: <xsl:otherwise>
To wit:
<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:choose>
<xsl:when test="wt:data_uri!= ''">
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of disable-output-escaping="no"
select="wt:data_uri"/>
</xsl:attribute>
<xsl:value-of select="wt:data_name"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="wt:data_name"/>
</xsl:otherwise>
</xsl:choose>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
I suspect that I will learn more graceful ways to do the test, but this works for now.
However, I really appreciate my first real response on this forum. I'm not being facetious. I really do appreciate it.
If this bothers people, then I'll stop. I just figured that people may appreciate a clear and simple log of the process of learning XSLT. It seems to be something in rather short supply out there.
In any case, I may be winding down anyway. I seem to have gotten to the point where I can pretty much customize the transform.
I'll start with a full XHTML page, a fragmentary XHTML page, move on to a WML 2.0 page, then do a WML 1.0 page (the WURFL stuff is very useful here). After that, I'll do an RSS 1.0 page (maybe I'll chicken out and do a 2.0), a JSON transform, then move on to Excel and Word dumps.
After all, this is exactly what XSLT is supposed to be good for. I'm creating an SDK for the site, and this looks like the ideal solution.
[edited by: encyclo at 2:13 pm (utc) on April 1, 2007]
In fact, Microsoft has even gone as far as designing an executable form of XML, sort of like XSLT, called XAML [msdn2.microsoft.com].
It was supposed to have been a major part of Vista, but I haven't heard very much about it lately. It may be fizzling.
It looks like I'm not doing things very efficiently. I want to see if there is a way to do subroutines and includes.
I have my hello world project operating exactly as I'd like. It currently produces very good XHTML 1.1, formatted well.
I need to start looking at how PHP uses XSLT. I have been using <oXygen> for all this work. I've heard that PHP XSLT has performance issues. As some of these dumps can get pretty big, that concerns me.
I want to see if there is a way to do subroutines and includes.
[w3schools.com...] gives example of a call passing parameters.
Their parameter mechanism is really odd.
Do you know how to set a parameter across iterations?
I have a loop, where I am displaying rows of information. I want to give them alternating CSS classes, so I need a param that remembers its value, and allows you to toggle it.
I see tons of stuff about how to set parameters from the invocation engine and when calling "subroutine" templates, but zip on how to use a parameter like a typical variable.
Basically, every example shows a parameter being a value that is affected from outside the context, and brought into the context. There is nothing about switching values within the same context.
Have you a case in mind the needs variable variables?
Yup. I need to produce an alternating display, like an old line printer paper output. It's a pretty standard way to display tabular data. Let's go back to my "wee test" example:
We'll expand it to six elements:
<?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>
<elementwrapper id="2">
<data_name>Test</data_name>

</elementwrapper>
<elementwrapper id="3">
<data_name>Test</data_name>
</elementwrapper>
<elementwrapper id="4">
<data_name>Test</data_name>

</elementwrapper>
<elementwrapper id="5">
<data_name>Test</data_name>

</elementwrapper>
<elementwrapper id="6">
<data_name>Test</data_name>
</elementwrapper>
</weetest>
Here's the current stylesheet:
<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 indent="yes" method="html" version="4.01" doctype-system="http://www.w3c.org/tr/html4/strict.dtd" 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">
<xsl:call-template name="render_one"/>
</xsl:for-each>
</body>
</html>
</xsl:template>
<xsl:template name="render_one">
<div>
<xsl:choose>
<xsl:when test="wt:data_uri!= ''">
<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>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="wt:data_name"/>
</xsl:otherwise>
</xsl:choose>
</div>
</xsl:template>
</xsl:stylesheet>
Here's how it renders so far:
<!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>
<div>Test</div>
<div>Test</div>
<div>
<a href="http://www.example.com/wee_test.html">Test</a>
</div>
<div>Test</div>
<div>Test</div>
</body>
</html>
Here's how I want it to render:
<!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 class="alt_0">
<a href="http://www.example.com/wee_test.html">Test</a>
</div>
<div class="alt_1">Test</div>
<div class="alt_0">Test</div>
<div class="alt_1">
<a href="http://www.example.com/wee_test.html">Test</a>
</div>
<div class="alt_0">Test</div>
<div class="alt_1">Test</div>
</body>
</html>
The standard way to do this is to have a class variable that you toggle with an if/then conditional.
<xsl:number value="position() mod 2" format="1" />
should give alternate 0 and 1
Here's the modified stylesheet:
<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 indent="yes" method="html" version="4.01" doctype-system="http://www.w3c.org/tr/html4/strict.dtd" 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">
<xsl:call-template name="render_one"/>
</xsl:for-each>
</body>
</html>
</xsl:template>
<xsl:template name="render_one">
<xsl:element name="div">
<xsl:attribute name="class">
<xsl:text>alt_</xsl:text>
<xsl:number value="position() mod 2" format="1" />
</xsl:attribute>
<xsl:choose>
<xsl:when test="wt:data_uri!= ''">
<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>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="wt:data_name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Thanks!
It just took looking at it from a different angle.