Examining XSLT
19th July 2005 · Last updated: 5th October 2016
I'm learning XSLT, a series of languages to process XML. Here's some of what I've picked up so far. Exciting stuff.
XSLT:
- Has over 100 built-in functions.
- Can add/remove elements
- Can rearrange/sort elements
- Can do tests on a document and decide which elements to display based on the result
- Can filter data based on criteria (such as equals, greater than etc)
- Client-side support is limited to IE6 and Mozilla/Firefox at present
- IE5 implemented it before the specs were written, so isn't fully W3C compliant
- Server-side works for all browsers (they just see the HTML produced at the end of it)
- PHP4 can be used to program in XSL, but not in the default installation, unlike PHP5
- ASP can be used if you're on a Windows server
Xpath:
- Useful for finding information in an XML document
- Able to navigate elements and attributes
- Can create XHTML from templates
- Has many CSS-like features such as these brief examples:
//title[@lang]
- Selects all title elements with an attribute 'lang'/shop/cd[price>12.99]
- Selects all CDs within the<shop>
element with a price greater than 12.99child::cd
- Selects all CD nodes that are the current node's childrenancestor
- Selects all ancestors of the current node, such as parents, grandparents and so on.
XQuery:
- Used for querying XML documents
- A simple format that returns the XML tags with it
- Can transform these into XHTML
- Can add attributes, elements and text
XPointer:
- Allows links in XML files to point to a specific part of a document (like HTML anchors)
XLink:
- Creates hyperlinks in XML
- Any element can be made a hyperlink!
- Allows for links to multiple destinations - pages or files
- Can determine when a linked item is loaded (eg: onload or onclick)
XSL-FO:
- For formatting XML
- Has familiar styles like CSS (borders, padding and margins etc)
- Defines regions for printing, such as headers and footers
The only thing I don't like much is XSL-FO. It seems verbose to me. Take this example from W3Schools (an excellent site for learning languages like XSLT). All it does is output a two-column table. Does the code really need to look like that? Aren't we in danger of losing the data itself in a sea of tags and attributes? (I think when something gets this complex, it's time to look at it from a new angle.)
<fo:table-and-caption>
<fo:table>
<fo:table-column column-width="25mm"/>
<fo:table-column column-width="25mm"/>
<fo:table-header>
<fo:table-row>
<fo:table-cell>
<fo:block font-weight="bold">Car</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block font-weight="bold">Price</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-header>
<fo:table-body>
<fo:table-row>
<fo:table-cell>
<fo:block>Volvo</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>$50000</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block>SAAB</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>$48000</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
</fo:table-and-caption>