RE: [xsl] xml cells transformed in to a 2-d table, some table cells missing, full table required - constant variables.

Subject: RE: [xsl] xml cells transformed in to a 2-d table, some table cells missing, full table required - constant variables.
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Sat, 3 Jan 2009 07:54:19 -0000
By the way, please don't get into the habit of writing:

<xsl:variable name="row">
  <xsl:value-of select="letter"/>

when you could write

<xsl:variable name="row" select="letter"/>

I'm afraid it's a very common but very bad coding habit, and it's as well to
break it early.

It's not just that your code is more verbose than it needs to be (XSLT has a
reputation for being verbose, which coding like this only encourages). More
to the point, it can be extremely inefficient. The two constructs are not
technically equivalent: in the first case you are building a tree structure
(a "result tree fragment" in 1.0 terminology), consisting of a document node
and a text node. It's very likely that when you refer to $row, you will do
so in a context where the string value of $row is required, in which case
the string will be extracted from the result tree fragment - so why build
this tree in the first place, when all you need is the string? 

Michael Kay

> -----Original Message-----
> From: Brendan Sleight [mailto:bms@xxxxxxxxxx] 
> Sent: 03 January 2009 02:01
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] xml cells transformed in to a 2-d table, some 
> table cells missing, full table required - constant variables.
> Hi All,
> I am having fun with XSL. I am developing an application for 
> traffic signals (traffic-lights) that uses xml and the input 
> and output. I want to make everything based around the xml. 
> So I decided to use stylesheets when I need text output 
> rather than create extra methods on the classes. Basically I 
> now being stubborn and am _going_ to use stylesheets even if 
> it drives me insane.
> I have hit a snag as variables in XSL are constant. So I can 
> not set a flag to tell me when something has not produced a output.
> I am taking a basic xml file:-
> Basically with lots of entries such as:-
>    <intergreen>
>     <from>A</from>
>     <to>B</to>
>     <length>7</length>
>    </intergreen>
>    <intergreen>
>     <from>A</from>
>     <to>C</to>
>     <length>5</length>
>    </intergreen>
>    <intergreen>
>     <from>B</from>
>     <to>A</to>
>     <length>7</length>
>    </intergreen>
> Each of the <intergreen> are going to be used to form a 2-d 
> table. So far I have got this stylesheet:-
> Normally I would use python-amara -
> as my XSL 
> processor, but I testing using xmlstarlet. 
>, version 1.0.1-2. The 
> command:- xmlstarlet tr ./site-transform-intergreens.xsl 
> ./example-site-2.xml
> Gives:-
> <?xml version="1.0"?>
> <b>Intergreen table</b><tt><i>
>  </i><i>  A</i><i>  B</i><i>  C</i><i>  D</i><i>  A</i><b> 7 
> </b><b> 5 </b><i>  B</i><b> 7 </b><i>  C</i><b> 6 </b><i>  
> D</i><b> 5 </b><b> 5 </b><b> 5 </b></tt>
> Or without the <b> and <i>, A table of "from" verus "to" :-
> Intergreen table
>    A  B  C  D
>  A 7  5
>  B 7
>  C 6
>  D 5  5  5
> However I can not find a solution where if i do not have en 
> entry for say, From A to D, a cell is still added to the 
> plain text table. (Long story but I can not use html tables.) 
> So rather then getting a half table I get a full populated 
> table, the ideal answer:-
> Intergreen table
>     A  B  C  D
>  A 0  7  5  0
>  B 7  0  0  0
>  C 6  0  0  0
>  D 5  5  5  0
> I could pre-populate the xml file with null entires, but that 
> makes the file on say a 32x32 table not very friendly for a 
> human to read.
> I have played around with a few options, but hit the snag 
> that variables in XSL are constants so flags are out of the 
> question. Also the stylesheet feel very poorly written by me, 
> as I loop through all the entries of the <intergreen> tag 
> until I find the tag equaling row and column, which feels 
> very in-efficient.
> So any tips on getting the stylesheet to the correct output 
> would be very appreciated as I have hit a bit of a brick wall 
> and run out of ideas. I am very new to style sheets and xml.  
> Can this even be done with stylesheets ?
> Best Regards,
> Brendan M. Sleight
> xmlstarlet tr ./processor-version.xsl ./exmaple.xml <?xml 
> version="1.0"?> <html 
> xmlns="";><head><title>XSLT
> Processor Version</title>
> </head><body><p>XSL version: 1.0
> </p><p>Vendor: libxslt
> </p><p>Vendor URL: </p><p>Product 
> name: [Undefined] </p><p>Product version: [Undefined] 
> </p><p>Is schema-aware: [Undefined] </p><p>Supports 
> serialization: [Undefined] </p><p>Supports backwards 
> compatibility: [Undefined] </p></body></html>

Current Thread