Forum Moderators: open

Message Too Old, No Replies

Conditionals

Okay, I didn't Need Variables, How Do We Decide Where to Go?

         

cmarshall

8:35 pm on Mar 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I now move on to making conditional decisions.

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.

cmarshall

10:33 pm on Mar 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just to clarify, what currently outputs 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>
<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>

daveVk

1:38 am on Mar 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[w3schools.com...]

Sorry to break monolog hope this is usefull

cmarshall

1:53 am on Mar 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



http://www.w3schools.com/xsl/xsl_if.asp

Sorry to break monolog hope this is useful

Yes, it was, but I needed an alternative path, so I chose

<xsl:choose>
[w3schools.com],
<xsl:when>
[w3schools.com] and
<xsl:otherwise>
[w3schools.com] instead:

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]

daveVk

12:08 pm on Mar 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My XSLT experience is limited to doing a few amazon web service transforms. I found the schema declaration in particular interesting as it is new ground for me, thank you.

cmarshall

12:35 pm on Mar 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, I'm planning on leveraging it to its fullest. As someone who has done a great deal of C++, XSLT is fairly simple and primitive, but, in the context of a data description language, it is actually pretty powerful.

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.

daveVk

12:58 am on Mar 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I want to see if there is a way to do subroutines and includes.

[w3schools.com...] gives example of a call passing parameters.

cmarshall

1:25 am on Mar 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks!

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.

daveVk

2:44 am on Mar 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[w3schools.com...]

Assume you need to move parameter to a variable to be able to change it

cmarshall

3:06 am on Mar 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Heh, heh.

In XML, a variable ain't.

(From the w3C Page):

Note: Once you have set a variable's value, you cannot change or modify that value!

I don't know what they were smoking when they came up with some of this terminology, but it must have been some primo stash.

daveVk

6:53 am on Mar 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Interesting.

So I guess we need to refactor without REAL variables.

Interaction is the issue, for-each probably solves some/most cases. For math type problems recursion is an alternative, assuming that is possible in XSLT.

Have you a case in mind the needs variable variables?

cmarshall

10:27 am on Mar 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.

daveVk

11:55 am on Mar 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



like [w3schools.com...] Example 4 with following change

<xsl:number value="position() mod 2" format="1" />

should give alternate 0 and 1

cmarshall

11:57 am on Mar 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wow. That might do it.

Thanks!

I'll report back when I give it a test.

cmarshall

12:19 pm on Mar 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yup. That did it.

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.

daveVk

3:17 am on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Luckily you wanted to list all, not sure how you would go about listing say the non links only, with same formating?

cmarshall

3:27 am on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good question. Without the ability to store a state, we're kinda screwed.

There quite literally seems absolutely no mechanism for persistent data storage, or a way of affecting stored data from within the stylesheet template.