RK* - rikkertkoppes.com

thoughts

Various XML related tips and tricks

I assume you have basic knowledge of what X(HT)ML is about, like knowing about how to properly serve the whole (with the correct MIME type) and knowing that for instance Internet Explorer will not understand XHTML. The issues below will probably not work when serving XHTML as HTML (which effectively makes it HTML, along with a HTML DOM and no namespace).

JS

Creating elements in another namespace

Suppose you do DHTML in an XHTML environment. So you are working with an XML DOM. Now you might want to create elements in another than the default (XHTML) namespace, for instance MathML or SVG elements, use the DOM level 2 namespace aware methods like createElementNS. For Internet Explorer you have to use the createNode() method.

XSL(T)

XSLT on XHTML documents

When you want to apply a transformation on an XHTML document (or any other document with a default namespace), you have to declare a prefix for that particular namespace in the XSL document and supply your Xpath expressions with that prefix:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
	xmlns:xhtml="http://www.w3.org/1999/xhtml"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<!-- xsl stuff -->
<xsl:value-of select="//xhtml:h1[1]"/>
<!-- more xsl stuff -->
</xsl:stylesheet>

Generating XHTML with XSL

The elements you generate with your XSL have to be in the XHTML namespace as well. Therefore, provide a default XHTML namespace for your XSL sheet. Also tell the parser not to include xmlns attributes for all those XHTML elements, since you will probably put that as a default namespace in the html element:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
	xmlns:xhtml="http://www.w3.org/1999/xhtml"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	exclude-result-prefixes="xhtml"
>
<!-- xsl stuff -->
</xsl:stylesheet>

XHTML to XHTML

Combine the above techniques to transform XHTML to XHTML. Also see xmltraining.biz: Transform XHTML to XHTML with XSLT on this.

You probably want to just copy the most of your XHTML, use the identity template:

<!-- the identity template -->
<xsl:template match="@*|node()">
	<xsl:copy>
		<xsl:apply-templates select="@*|node()"/>
	</xsl:copy>
</xsl:template>

This wil just copy everything. Now make additional templates for the things you want to change.

Extending XSLT with user functions in IE (msxsl processor apps)

The "X" is XSLT and Xpath has a meaning, in theory this provides a way to extend your XSL template with your own functions and elements (see the W3C XSLT spec on extensions). Microsofts msxsl processor supports this, see the following example:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:ext="http://xmlns.rikkertkoppes.com/xslextension"
	xmlns:msext="urn:schemas-microsoft-com:xslt"
	extension-element-prefixes="ext"
>

	<msext:script language="javascript" implements-prefix="ext">
		<![CDATA[
			function yeahbaby() {
				return "hello world";
			}
		]]>
	</msext:script>

	<xsl:template match="/">
		<html>
			<p><xsl:value-of select="ext:yeahbaby()"/></p>
		</html>
	</xsl:template>
</xsl:stylesheet>

In the xsl:stylesheet element I add an extension namespace (invent your own) and the Microsoft-provided urn:schemas-microsoft-com:xslt namespace, which just tells the msxsl parser the particular element is an extension element. Furthermore I have to tell the processor my own namespace is an extension namespace for the xsl document, not a namespace in the xml document being generated note that this is note strictly necessary when you only add functions, as in this example, which is done with the extension-element-prefixes attribute.

Thus far, this is all according to w3c specs and entirely general, so no matter what processor you use, this is the method to begin your extension (apart from the microsoft namespace probably). Now there's the msxsl only msext:script element (read: script element in the urn:schemas-microsoft-com:xslt namespace) which allows you to add your own functions. Note the language attribute, instead of the type attribute you should be used to nowadays. Also note the implements-prefix attribute, which tells in which namespace this functions will end up. Now script your functions.

Finally call these functions in xpath expressions as you are used to, but add your namespace prefix to it.

I have not yet found a way to extend gecko's xsl processor.

Grouping lists

If you have a list, say a list of people and you want to group them by surname, use the muenchian method.

This is also good for making alphabetic lists and similar grouping problems

Xpath

Selecting maximum or minimum values

Since min() en max() functions are not (yet) in xpath, you can use the following trick to get the minimum or maximum value:

n[not(../n &lt; .)]

Where n is the node you want. This effectively gives you the node for which there is no node smaller than itself, which is the node with the minimum value.

RDF

The about attribute

In contrast to what is said on the web in many places, the about attribute (of the Description element) has to be in the RDF namespace, so mostly prefixed.

Additional resources (top 15)

Below is a list of additional resources that might contain extra information about the subject at hand. These are all sites linking to this one (i.e. backtracking).

  1. xslt.startpagina.nl (112)
older articles

AdministrationAtom feed