September 14, 2007 at 5:03 am
I'm playing around with XML queries and found something interesting. If I use the column configurations with XML PATH, I can add ELEMENTS to the query, but the ELEMENTS is ignored. If I do a normal SELECT and just put "FOR XML PATH, ELEMENTS" at the end of the statement, the XML PATH acts like XML RAW (sorry, I typed the wrong thing in the title of my post and now I can't change it).
Why would XML PATH act like XML RAW? Is there a reason to ever use XML RAW if the PATH can do both its own thing (DBA scalable formatting) and RAW formatting?
What are your thoughts and experiences on the best one for which situations?
September 14, 2007 at 5:52 am
XML PATH has been introduced in 2005 only. XML RAW was part of 2000 too. XML PATH is much more flexible
Regards,
Andras
September 14, 2007 at 7:24 am
I always found the FOR XML RAW to be a bit broken, since it tends to return invalid XML (no root node, so any query returning more than one row would be "bad" xml). Not sure what they were thinking with that.
Assuming you are using 2005, XML PATH does seem to be the best choise (XML EXPLICIT is good, but tends to require a LOT of coding )
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
September 14, 2007 at 10:11 am
So, basically I'm correct in thinking that if I don't need to return results as one row XML (FOR XML RAW without the Elements keyword), that there's really no reason to use XML RAW at all, right?
And yes, I'm using 2005.
BTW, PATH doesn't seem to return a "real" root node unless you specific ROOT. In fact, PATH will complain about an invalid root if you don't specific ROOT and get more than 1 result row from your query. If I double-click and expose the XML in SSMS, the second root element line has a red squiggly line under it. Hovering my mouse over that element name, I get The error "XML document cannot contain multiple root level elements".
September 14, 2007 at 11:55 am
I like PATH better these days, because it gives me the control needed as to what the "guts" of the XML file should look like by playing with the field labels (which XML AUTO and RAW ignore), but without having to write some ungodly UNION query like XML EXPLICIT tends to force you to do. So you can build a multi-level XML file this way with some amount of control over what it will look like. Besides - I just can't "deal" with every node always being called "row" - I'd at least like to name THAT.
And - agreed on the ROOT predicate.
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Viewing 5 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply