Andy Fish wrote:
Hi,
I just found this template in someone else's xslt (it's Microsoft's
"word2html" stylesheet to convert WordProcessingML to HTML)
<xsl:template match="WX:sect">
<xsl:variable name="thisSect" select="."/>
<div>
<xsl:for-each select="//WX:sect">
<xsl:if test=".=$thisSect">
<xsl:attribute name="class">Section<xsl:value-of
select="position()"/></xsl:attribute>
....
It seems that the author is looping through all the WX:sect nodes looking
for the context node, in order to extract the position. However, I don't
understand the <xsl:if> test. According to the xpath spec:
"If both objects to be compared are node-sets, then the comparison will be
true if and only if there is a node in the first node-set and a node in the
second node-set such that the result of performing the comparison on the
string-values of the two nodes is true".
Yes, it doesn't work, strictly speaking.
I simplified the stylesheet (namwspaces away, and a little guessing):
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="doc">
<toc>
<xsl:apply-templates/>
</toc>
</xsl:template>
<xsl:template match="sect">
<xsl:variable name="thisSect" select="."/>
<div>
<xsl:for-each select="//sect">
<xsl:if test=".=$thisSect">
<xsl:attribute name="class">Section<xsl:value-of
select="position()"/></xsl:attribute>
</xsl:if>
</xsl:for-each>
<stupid-summary>
<xsl:value-of select="."/>
</stupid-summary>
</div>
</xsl:template>
</xsl:stylesheet>
With the input doc:
<doc>
<sect>
<title>About foo</title>
<para>foo is green</para>
</sect>
<sect>
<title>About bar</title>
</sect>
<sect>
<title>About baz</title>
</sect>
<sect>
<title>About foo</title>
<para>Correction: foo is red</para>
</sect>
<sect>
<title>About bar</title>
</sect>
</doc>
it transforms (xsltproc) to:
<?xml version="1.0"?>
<toc>
<div class="Section1"><stupid-summary>
About foo
foo is green
</stupid-summary></div>
<div class="Section5"><stupid-summary>
About bar
</stupid-summary></div>
<div class="Section3"><stupid-summary>
About baz
</stupid-summary></div>
<div class="Section4"><stupid-summary>
About foo
Correction: foo is red
</stupid-summary></div>
<div class="Section5"><stupid-summary>
About bar
</stupid-summary></div>
</toc>
The thing compared is the text value of the node sets (as you wrote).
They are the same for the 2nd and 5th sect elements -- so two attributes
with the same name were output -- the last one won; there is a section
5 after section 1.
Of course, having the same title child but some different (text values
of) other children (1st and 4th sect elements) WILL distinguish the text
values of the nodes.
Soren