XPath for attribute value

After struggling with attributes for a long time, I finally discovered the easy way to obtain the text value of an attribute in a XML file with a XPath expression. Let’s say you got the following XML-file

  <note id="501">
    <body>Don't forget me this weekend!</body>
  <note id="502">
    <heading>Re: Reminder</heading>
    <body>I will not</body>

To get the value of the id attribute of the first note use an Xpath-expression with a string function

[ string( //note[1]/@id ) ]
and you get 501.