Monday, February 14, 2011

Stuff that I only use once every 6 months: XPath

It became convenient to manipulate some XML inside of a field in Sql Server (2008 r2) recently.
I found the XML datatype and the modify function which operates using XQuery. XQuery is a superset of XPath and both are w3c standards.
The silly field wasn't the XML data type already, it was just an nchar stuffed full of Xml.
The query that I wrote looked something like this when I was done:

1 CREATE TABLE T(TheXml XML)
2 GO
3
4 INSERT T SELECT CAST(code as XML) FROM some_table WHERE name = 'that thing that I was looking for'
5 GO
6
7 UPDATE T SET TheXml.Modify('delete /Object/Object/Object[@name="That one name"]/../Object')
8 GO
9
10 DROP TABLE T
11 GO

Most of my time was spent in the act of realizing that I needed to look for help on XPath and not some weird microsoft thing. The syntax that I needed (in this case "..") was easily found once I googled XPath.

1 comment: