I have a XML file that looks like this:
<container>
<bugs>
<bug id="b1">
<reporter>Tom</reporter>
...
</bug>
<bug id="b2">
<reporter>Jane</reporter>
...
</bug>
</bugs>
<users>
<user>
<userid>10</userid>
<username>Tom</username>
</user>
<user>
<userid>5</userid>
<username>Jane</username>
</user>
</users>
</container>
The endresult should be:
<items>
<item>
<bugid>b1</bugid>
<author id="10">Tom</author>
</item>
<item>
<bugid>b2</bugid>
<author id="5">Jane</author>
</item>
</items>
Problem: I can’t get the comparison correct for the id lookup.
I assumed it would be like this:
<xsl:template match="bug">
....
<xsl:element name="author">
<xsl:attribute name="id">
<xsl:value-of select="//users/user[username=reporter]/userid"/>
</xsl:attribute>
</xsl:element>
....
</xsl:template>
But this returns an empty id ( id=”” ).
<xsl:value-of select="//users/user[username='Tom']/userid"/>
returns 10
and
<xsl:value-of select="reporter"/>
returns Tom.
I don’t see what’s wrong here.
looks for a
usercontaining equalusernameandreporterelements. It does not search for thereporterin yourbug.You can get this to work by binding the reporter’s name to a variable first: