Title: Comparing attribute values in XPath
Tags: xslt;xpath
Question: I want to get the value of a specific node, specified by its id attribute. But the behaviour of my XSL parser, Saxon, is not how I expected it to work.
This is my XSL code:
<xsl:template match="synonyme"&gt;
<xsl:element name="corr"&gt;
<xsl:value-of select="@connecteur" /&gt;
<xsl:value-of select="/liste/connecteur[@id=@connecteur]/forme[1]" /&gt;
</xsl:element&gt;
</xsl:template&gt;
I just matched a tag named synonyme which has a connecteur attribute. My code outputs the value of this attribute.
I also want to output the value of another node which has an id attribute matching the connecteur attribute of my currently matched synonyme tag. But no results are ever found for this query, the second value-of always has empty output.
If I write, e.g. [@id='c160'], where c160 is the exact same thing that is output by the first value-of, it works! But not when comparing to the @attribute of my matched node. How can I fix this?
The XML is basically
<liste&gt;<connecteur id="c160"&gt;<forme&gt;foo</forme&gt;</connecteur&gt;
<connecteur id="c161"&gt;<synonyme connecteur="c160" /&gt;</connecteur&gt;
</liste&gt;
and the expected output in place of the synonyme is <corr&gt;c160 foo</corr&gt;.

Here is the accepted answer: The predicate you use:
[@id=@connecteur]
is looking for an element with two attributes - id and connecteur- with equal values. To look for an element with an id attribute whose value matches the value of the current element's connecteur value, you need to use:
[@id=current()/@connecteur]
See: https://www.w3.org/TR/xslt/#function-current
A better solution would be to define  a key as:
<xsl:key name="ref" match="connecteur" use="@id" /&gt;
then use:
<xsl:value-of select="key('ref', @connecteur)/forme" /&gt;
to resolve the cross-reference.
See: https://www.w3.org/TR/xslt/#key


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:11/20/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<xslt><xpath>', 'Title': 'Comparing attribute values in XPath', 'ViewCount': 2248, 'id': 40701099, 'user_AboutMe': '<p>Computational Linguistics B.Sc. from University of Potsdam, Germany.</p>\n<p>7 years of experience developing Windows software for a backup/office software company (languages: classic and modern C++, VB6, Python, PHP)</p>\n<p>4 years at TomTom, developing embedded routing software on Linux in modern C++.</p>\n', 'user_AccountId': 222458.0, 'user_DisplayName': 'Felix Dombek', 'user_Reputation': 12968.0, 'user_Views': 1780.0, 'user_id': 481061.0}}