SQL Server XPath/XQuery Exercise

Status
Not open for further replies.
Hello Again All,

I'm writing with a question that ostensibly relates to my work.

Let's say I am storing an XML blob in my database that looks like this:
<Root>
<Element name="Blah" type="Magic" foo="bar" blech="test" .../>
<Element name="Wow" type="Normal" foo="bar" blech="test" .../>
...
</Root>

I'm retrieving this XML and storing it in an XML typed variable (SQL Server 2008).

What I'm trying to do is query this XML variable so that I get just this:
<Root>
<Element name="Blah" type="Magic" />
<Element name="Wow" type="Normal" />
</Root>

In other words, I only want to strip out all of the attributes on my "Element" elements except for "name" and "type".

I've already got this working by doing this:
Code:
SELECT	t.value(N'@name[1]', N'nvarchar(64)') AS [@name],
			t.value(N'@type[1]', N'nvarchar(64)') AS [@type]
FROM @XMLVar.nodes(N'/Root/Element') AS Fields(t)
FOR XML PATH(N'Element'), ROOT(N'Root')

But I'd like to see how it can be done using query() syntax like this:
Code:
SELECT @XMLVar.query(N'for $a in /Root/Element (???) select $a')

This is merely to improve my xquery knowledge, if it would be a huge hassle to do, I can easily move on but I know just enough about xquery that I think this should be (relatively) simple, but not enough to actually figure it out.

Thanks
 
Status
Not open for further replies.