The first title
element in the document is selected by:
(//title)[1]
Many people mistakenly think that //title[1]
selects the first title
in the document and this is a frequently committed error. //title[1]
selects every title
element that is the first title
child of its parent -- not what is wanted here.
Using this, the following transformation produces the required output:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"title[count(.|((//title)[1])) = 1]">
<PageTitle>
<xsl:apply-templates />
</PageTitle>
</xsl:template>
</xsl:stylesheet>
When applied on this XML document:
<t>
<a>
<b>
<title>Page Title</title>
</b>
</a>
<b>
<title/>
</b>
<c>
<title/>
</c>
</t>
the wanted result is produced:
<t>
<a>
<b>
<PageTitle>Page Title</PageTitle>
</b>
</a>
<b>
<title />
</b>
<c>
<title />
</c>
</t>
Do note how we use the well-known Kaysian method of set intersection in XPath 1.0:
If there are two nodesets $ns1
and $ns2
, the following expression selects every node which belongs to both $ns1
and $ns2
:
$ns1[count(.|$ns2) = count($ns2)]
In the specific case when both node-sets contain only one node, and one of them is the current node, the following expression evaluates to true()
exactly when the two nodes are identical:
count(.|$ns2) = 1
A variation of this is used in the match pattern of the template that overrides the identity rule:
title[count(.|((//title)[1])) = 1]
matches only the first title
element in the document.