
USE xmldb
GO

SELECT xml_col.query('/people/person') AS persons
  FROM xml_tab
GO


INSERT xml_tab VALUES(
 '<people>
    <person name="laurel"/>
    <person name="hardy"/>
  </people>')
GO

SELECT xml_col.query('/people/person') AS persons
  FROM xml_tab
GO

SELECT xml_col.query('
   for $b in /people/person 
   return ($b)
 ') 
 FROM xml_tab
GO

-- XQuery WHERE clause
SELECT the_id, xml_col.query('
   for $b in /people/person 
   where  $b/@name = "moe"
   return ($b)
 ') AS persons 
 FROM xml_tab

-- XPath predicate
SELECT the_id, xml_col.query('/people/person[@name="moe"]') AS persons
  FROM xml_tab
GO

DROP TABLE xml_tab
GO


CREATE TABLE xml_tab(
  the_id INTEGER PRIMARY KEY IDENTITY,
  xml_col XML)
GO

INSERT xml_tab VALUES('<people><person><name>curly</name></person></people>')
INSERT xml_tab VALUES('<people><person><name>larry</name></person></people>')
INSERT xml_tab VALUES('<people><person><name>moe</name></person></people>')

INSERT xml_tab VALUES(
 '<people>
    <person><name>curly</name></person>
    <person><name>larry</name></person>
    <person><name>moe</name></person>
    <person><name>moe</name></person>
  </people>')
GO

-- literal result element and
-- constructor with curly braces
SELECT xml_col.query('
   for $b in //person 
   where  $b/name="moe"
   return <li>{ string($b/name[1]) }</li>
 ') as query_for_moe
 FROM xml_tab
GO
