XPath query on XML : XML query « XML « SQL Server / T-SQL Tutorial






2> CREATE TABLE dbo.Contacts
3> (
4>   contactid       INT          NOT NULL PRIMARY KEY,
5>   contactname     NVARCHAR(50) NOT NULL,
6>   I18N        BIT          NOT NULL,
7>   otherattributes XML          NOT NULL
8> );
9> GO
1>
2> INSERT INTO dbo.Contacts VALUES(1, N'Mike', 1, N'<I18N xmlns="I18N"><ID>0</ID><FL>Spanish</FL></I18N>');
3> INSERT INTO dbo.Contacts VALUES(2, N'Her', 0, N'<Foreigns xmlns="Foreign"><NL>German</NL><ENG>1</ENG></Foreigns>');
4> INSERT INTO dbo.Contacts VALUES(3, N'Ric', 1, N'<I18N xmlns="I18N"><ID>1</ID><FL>German</FL></I18N>');
5> INSERT INTO dbo.Contacts VALUES(4, N'Gianluca', 0, N'<Foreigns xmlns="Foreign"><NL>Italian</NL><ENG>1</ENG></Foreigns>');
6> GO

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)
1>
2> SELECT contactid, contactname,
3>   otherattributes.query('
4~     declare namespace D="I18N";
5~     /D:I18N/D:FL/text()') AS languagespoken
6> FROM dbo.Contacts
7> WHERE I18N = CAST(1 AS BIT);
8> GO
Msg 1934, Level 16, State 1, Server J\SQLEXPRESS, Line 2
SELECT failed because the following SET options have incorrect settings: 'QUOTED_IDENTIFIER'. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or q
uery notifications and/or xml data type methods.
1>
2> drop table dbo.Contacts;








24.4.XML query
24.4.1.XML query on XML column
24.4.2.XPath query on XML
24.4.3.SELECT @ProductBillingXML.query('/ProductBilling/OrderItems/Item')
24.4.4.Retrieve XML data using XPath
24.4.5.XPath search and element index