Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
As we will see, this
So let's look at the template rule for the
match=“rhs[preceding-sibling::*[1][self::lhs]]”
, but the writer of this stylesheet chose to handle both cases in a single rule, like this:
select=“following-sibling::*[1][name()=‘com’ or
name()=‘constraint’ or
name()=‘vc’ or
name()=‘wfc’]”/>
select=“following-sibling::*[1][name()=‘com’ or name()=‘constraint’ or name()=‘vc’ or name()=‘wfc’]”/>
Once again, the code is cluttered by the
What the code does is this:
Some people prefer to avoid empty table cells by writing
Finally, the last column contains the representation of a
After studying the previous rule, the basic structure should be familiar. But there is some extra code included in this rule, because the
def
attribute, and this is used directly to construct the HTML internal hyperlink. The displayed text of the link is formed by retrieving the element whose
ID
is equal to this
def
attribute, and displaying its text.
So much for formatting the production rules! This is by far the most complicated part of this stylesheet; the rest should be plain sailing. But before we move on, we should ask whether all this logic could have been written in a more straightforward way in XSLT 2.0.
I see this problem as an example of a positional grouping problem. Grouping problems are all concerned with turning a one-dimensional sequence of elements into a hierarchy, and the problem of arranging data in a table can often be understood as a grouping problem in which the hierarchic levels are the table, the rows, and the individual cells.
All grouping problems can be solved by answering two questions:
We already have answers to these questions in the existing stylesheet: the group is a row of the table, and we have an XPath expression that selects elements that will be the first in a new row. The other elements in the row are then the following siblings, up to the next element that's a “new row” element.
So here is my XSLT 2.0 solution to this problem. First, in the two template rules for
match=“prod”
and
match=“prodgroup/prod”
, we'll replace the complicated
show.prod
, with no parameters. This template looks like this: