[xquery-talk] Brainteaser

Jason Hunter jhunter at servlets.com
Tue May 13 15:49:52 PDT 2003


Here's a brainteaser XQuery problem, borrowed from a co-worker's real
problem from this afternoon.  I thought I'd share it as a little XQuery
braincell stretch.  I'm not asking for help; I know the answer and will
post it later in the off chance no one else writes in.  (Don't cheat and
peek ahead unless you need to!)

Assume an XML file "teaser.xml":

  <root>
    <record sequence="1">
      <name>One</name>
      <date sec="1052762855">2003-05-12T18:07:35Z</date> 
    </record>
    <record sequence="2">
      <name>Two</name>
      <date sec="1052762941">2003-05-12T18:09:01Z</date>
    </record>
  </root>

The following query contains three enclosed expressions, only one of
which actually works as intended.  The comments in the query describe
the situation.  Who can figure out the reason?

define function get-root() as element {
  document("teaser.xml")/root
}

<html xmlns="http://www.w3.org/1999/xhtml">
  <body>
    <br> This correctly prints the original document
      {
        get-root()
      }
    </br>

    <br> This should print the original document, but doesn't
      {
        document("teaser.xml")/root
      }
    </br>
        
    <br> This should print the names, but doesn't
      {
        get-root()/record/name
      } 
    </br>
  </body>
</html>


-jh-


More information about the Talk mailing list