Note that there are some explanatory texts on larger screens.

plurals
  1. POIncluding an attribute of an unrelated element in an XPath
    text
    copied!<p>I have the following XML file:</p> <pre><code>&lt;phonebook&gt; &lt;departments&gt; &lt;department id="1" parent="" title="Rabit Hole" address="" email="" index=""/&gt; &lt;department id="2" parent="" title="Big Pond" address="" email="" index=""/&gt; &lt;/departments&gt; &lt;employees&gt; &lt;employee id="1" fname="Daffy" lname="Duck" title="Admin" email="daffy.duck@example.com" department="2" room="" /&gt; &lt;employee id="2" fname="Bugs" lname="Bunny" title="Programmer" email="bugs.bunny@example.com" department="1" room="" /&gt; &lt;/employees&gt; &lt;/phonebook&gt; </code></pre> <p>When displaying it, I want to show the contact details for an employee as well as the title of the department where he works. Here's what I've got in the template:</p> <pre><code>&lt;xsl:for-each select="phonebook/employees/employee"&gt; &lt;xsl:sort select="@lname" /&gt; &lt;tr&gt; &lt;td&gt; &lt;span class="lname"&gt;&lt;xsl:value-of select="@lname"/&gt;&lt;/span&gt; &lt;xsl:text&gt; &lt;/xsl:text&gt; &lt;span class="fname"&gt;&lt;xsl:value-of select="@fname"/&gt;&lt;/span&gt; &lt;/td&gt; &lt;td&gt;&lt;xsl:value-of select="@title"/&gt;&lt;/td&gt; &lt;td&gt; &lt;xsl:value-of select="/phonebook/departments/department[@id='{@department}']/@title"/&gt; &lt;/td&gt; &lt;td&gt;&lt;a href="mailto:{@email}"&gt;&lt;xsl:value-of select="@email"/&gt;&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/xsl:for-each&gt; </code></pre> <p>The problem is that the following rule doesn't seem to work:</p> <pre><code>&lt;xsl:value-of select="/phonebook/departments/department[@id='{@department}']/@title"/&gt; </code></pre> <p>I guess this is because the XSLT engine looks for the <code>department</code> property in the <code>department</code> element, and not in the <code>employee</code> element. However, I don't have an idea how to fix it. Could anyone give me a hint on that?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload