views:

174

answers:

2

What are the hidden features of XPath AND XSLT?

+2  A: 

A dirty little hack, to start things off:
Conditional strings in Xpath by abusing the fact that true is 1 and false is 0.

Example:

<div class="ID{substring('-on', 4-4*($Var = 2))}">

Yields <div class="ID-on"> or <div class="ID">, depending on if $Var is 2.

Brian
This is called "Becker's method".
jelovirt
+5  A: 

document('')

Accessing the XSLT document itself with document(''). Occasionally useful for stuff like accessing lookup tables stored inside the stylesheet itself, eliminating the need for an external file.

Jukka Matilainen
I'm not sure I'd really consider that a "hidden" feature, given that it is clearly stated in the spec: *'document("") refers to the root node of the stylesheet; the tree representation of the stylesheet is exactly the same as if the XML document containing the stylesheet was the initial source document'* - http://www.w3.org/TR/xslt#document Still, a surprising number of people don't know it, so +1 anyway :-)
NickFitz