xslt predicates, value-of

Syntax

1. Predicates and children
2. xsl:value-of vs xsl:apply-templates
3. xsl:if test question
4. Syntax Highlighting

1.

Predicates and children

David Carlisle

match="foo[@bar]" finds foo elements that have are children of the current node, and have a bar attribute

match="foo/@bar" finds bar attributes of foo children of the current node

if this is in an apply-templates, then the current node in the matching template will be the foo element in the first case but an _attribute node_ in the second.

match="foo/@bar" does not mean `children' it means `next step' and at each step you can change axis along which to travel, here you have @ which is short for attribute::, and no specified axis is short for child:: so the above is really

child::foo/attribute::bar

which means get bar attributes of foo children.

2.

xsl:value-of vs xsl:apply-templates

David Carlisle

Using value-of just gets you the character data, so if there is any xml markup in the content of the current element then that won't get output from that markup.

3.

xsl:if test question

David Carlisle

  <xsl:when test="$counter<'1'">

'1' is the string 1, on which you can't do a less than test. You want the number 1, so use

  <xsl:when test="$counter < 1">
  > What I am trying to do is simply to test if the number of ELT is less
  > than 1

If that's all you want to do with the count, you don't need a variable at all:

    <xsl:when test="count(ELT) = 0">

since it's a count frrom a node list, it can't be negative, so = 0 seems a simpler test than < 1.

In fact that is just testing whether there are any ELT, for which you don't need count() as an empty node list counts as false,

    <xsl:when test="ELT">

4.

Syntax Highlighting

Jirka Kosek et al


> On a  side note I really liked the web-based syntax highlighting and
> coloring, makes it a lot easier to read code. I was looking for a
> program that does the coloring for online code presentation, I've come
> across Java2HTML converter, I was hoping there was something like that
> for XSLT also.

You mean XSLT code for syntax highlighting? I'm not aware of pure XSLT based solution (although with XSLT 2.0 RE support you can do a lot), but there is Saxon extension XSLTHL (http://sourceforge.net/projects/xslthl) which can be used for syntax highlighting. It is also integrated in DocBook XSL stylesheets: xmlguru.cz

Jacek Radajewski adds

I wrote my own templates to do generic XML syntax highlighting, but then found XML verbatim (HTML output) by Oliver Becker - http://www2.informatik.hu-berlin.de/~obecker/XSLT/

I've created an equivalent FO stylesheet and added support for coloring prefixes by name and ability to select (via XPath) XML elements for emphasis.

If you want to have a look at work in progress please visit: sourceforge sourceforge and sourceforge

The stylesheets can be found here sourceforge