Why XSLT name() Function is Evil

XSLT name() function is evil and should be avoided just like GoTo.

For example, it is very bad to write

<xsl:apply-templates select="*[name()!='a:b']"/>

because it will exclude both of

<a:b/>
<a:b xmlns:a="totallyDifferentNamespace"/>

the correct way to write above apply-templates is

<xsl:apply-templates select="*[not(self::a:b)]"/>

or in XSLT 2.0

<xsl:apply-templates select="* exclude a:b"/>

assuming xmlns:a="someSpecificNamespace"

Test code is attached below.

AttachmentSize
xslt-evil-name.zip2.58 KB

Comments

You should not be using

You should not be using name() function even if you have namespaces turned off.

Saxon (and perhaps some other processors) contain an optimization that translates XML qualified names into packed integer structures (see NamePool in Saxon).

So, using name() function bespoke potential problems also makes your XSLT code slow.

Post new comment

The content of this field is kept private and will not be shown publicly.