SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


TSQL- XQuery


TSQL- XQuery

Author
Message
matthew.flower
matthew.flower
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1080 Visits: 359
I also don't see the trick, but even if "data" has been added properly it would (or should) still have given an error because "<a>1<c>3</c><d>4</d></a>" is not valid XML: <a> is obviously a complex element because it contains <c> and <d>, so it can't contain "1".
Toreador
Toreador
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3824 Visits: 8123
matthew.flower (7/29/2013)
<a> is obviously a complex element because it contains <c> and <d>, so it can't contain "1".


Where did you get that idea from? It's perfectly valid.
Tom Thomson
Tom Thomson
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25871 Visits: 12494
I answered this question yesterday (as often happens Monday's question was available on Sunday) and didn't see anything in it that needed comment, Then today here piles of comments calling it a trick question. And one sensible comment:
Hugo Kornelis (7/29/2013)
I like the question. I don't think it's a trick question

I agree, it's not. There are things which are case sensitive, and this is one of them so there's no trick here.
many people here are so used to using case insensitive collations (stupid defaults!!!) that they can and will be caught by this XML feature.

Some of us expect case insensitivity as the norm, but know that we have to be aware when something is case sensitive. So we don't get caught - at least not after the first time. And when its associated with something else pretty awful - two pretty awful things here: XQuery and XML - and especially when the case sensitivity is itself awful because it's utterly pointless, so that we have three pretty awful things in conjunction, it is memorable because of it's sheer awfulness.
Probably numerous times.

No, just once.
And the second good thing about the question is that it also makes you think about what would have been returned if the case had been correct. I know I was not really sure of that (I'm far from an XML expert!), so I was happy that the lower-/uppercase thing gave me a guaranteed point, and the rest of the question gave me the opportunity to play with the code and see what happens if I change the case - so this has also taught me something about the XQuery data function. (And if I just happen to stumble on it again within a week or so, it might even stick this time).

I have some vague recollection that "data" means do a top-start depth-first left-to-right traverse of the subtree consisting of the designated node and all its descendants, starting with no data and at each node concatenating the data of that node (if any) to the data accumulated so far (concatenating it to an empty string of data if none has yet been accumulated) and returning the accumulated data with no indication of where the boundaries between the data of one node and another node might be (i.e. totally devoid of structural information). That might actually be wrong, but I don't care because if I need to know I can either play (if I have a SQL Server instance I can play with handy) or look it up (if I have internet access handy) and it's pretty unlikely that I would need to know in any situation where either of those facilities was absent, let alone both. But it's probably about right as it means that if "data" had been spelt right the two XQuerys would have produced the result given as the first wrong answer option, which would make that option a good distractor - another reason not to dislike the question despite its horrible subject matter.

Tom

Tom Thomson
Tom Thomson
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25871 Visits: 12494
Toreador (7/29/2013)
matthew.flower (7/29/2013)
<a> is obviously a complex element because it contains <c> and <d>, so it can't contain "1".


Where did you get that idea from? It's perfectly valid.

+1000.

XML would be pretty useless if non-leaf nodes couldn't contain data. (Actually, it's already rather more useless than many of those who misuse it tend to think :-D.)

It's name, eXtended Mark-up Language, makes it pretty clear that it has to be able to put mark-up at any point in the structure it's annotating. So I guess that for something where annotation of non-leaf constructs is required XML without data at non-leaf nodes would have to describe the whole structure at the leaf level, which could be orders of magnitude more clumsy and verbose than XML is. I wonder if the strange idea expressed by Matthew is widespread? If so it might account for some of the really bad XML that can be found out in the wild.

Tom

Hugo Kornelis
Hugo Kornelis
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18723 Visits: 12426
L' Eomot Inversé (7/29/2013)
if I need to know I can either play (if I have a SQL Server instance I can play with handy) or look it up (if I have internet access handy)

True. The reason I still think it's good to know is not for the scenario where you get someone else's code and are wondering what the data function does (I would handle that exactly the same way you do), but for the scenario where you are given a task that might benefit from this function. Admittedly, the behaviour of the function is weird enough that it's hard to think of a realistic use case - but if you do run into one and don't remember that such a function exist, you might find yourself working very hard to reproduce this behavior using other XQuery expressions. Whereas in that situation, if you do happen to know that there is a function to do just that (even if you perhaps forgot the name and have to google for it), you'll save a lot of time and effort.

I share your antipathy (or should I say hatred?) of XML and XQuery. But matter of fact is, it is a part of the product I work with, and it's my professional responsibility to ensure I have at least sufficient working knowledge to handle things if they come up.


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Tom Thomson
Tom Thomson
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25871 Visits: 12494
Hugo Kornelis (7/29/2013)
I share your antipathy (or should I say hatred?) of XML and XQuery.

I think "dislike" is perhaps a better word than either of those. In the case of XML, I know that my dislike is irrational because it is based not on the XML definition but instead on all the horrible misuse of XML that has been caused by pundits who have hyped it beyond all reason as a panacea for everything.
But matter of fact is, it is a part of the product I work with, and it's my professional responsibility to ensure I have at least sufficient working knowledge to handle things if they come up.

When I was working full time, I shared that view; even now, I have enough knowledge that if I wanted to do a t-d d-f l-to-r data only scan of some XML in T-SQL I would know that @xml_thing.query('data(node)') was something that might do it, so I would know where to start looking.

Tom

Miles Neale
Miles Neale
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4222 Visits: 1695
L' Eomot Inversé (7/29/2013)
Hugo Kornelis (7/29/2013)
I share your antipathy (or should I say hatred?) of XML and XQuery.

I think "dislike" is perhaps a better word than either of those. In the case of XML, I know that my dislike is irrational because it is based not on the XML definition but instead on all the horrible misuse of XML that has been caused by pundits who have hyped it beyond all reason as a panacea for everything.


I am a little foggy on this but thought I should throw it out there. I had thought that XML was first built as a dataform to transfer data in an agreed upon schema between two data sharing partners. As such the schema could be very complex and specialized. Some where along the way the idea came to store this probably as an audit train for online transactions. This was due to probably legal requirements to keep the original transactions as they came to the server/service.

Originally stored as string of data, the parsing and query of the data within the schema was done by the application and simply stored as string data in databases. Then came a new age of enlightenment where data servers no longer saw this data only as strings but as data within data controlled by external knowledge and standardized schema proforma.

What then was at one point orderly data parsed into the database, became numerous xml fields each acting as a data heap or unformatted data collection that may or may not conform to any published schema. Truly this added security to the use of XML because no one knew what was going on. It also added to the complexity of storing and retrieval of data, let alone the ability to query the data structures within the heap.

So what once was and really is still today a very nice instrument for carrying data from point a to point b has now become a playfield for those who wish to define their own rules and order, but what does it do the maintainer of the process/database?

M.

Not all gray hairs are Dinosaurs!
Revenant
Revenant
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12451 Visits: 5010
I got it right, but probably only because a year ago I was writing XML parsers.

A good way to start a week. Thanks, Pramod!
Black_Knight
Black_Knight
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1499 Visits: 64
Hugo Kornelis (7/29/2013)
I like the question. I don't think it's a trick question - many people here are so used to using case insensitive collations (stupid defaults!!!) that they can and will be caught by this XML feature. Probably numerous times. I know I have, it's probably the main reason I got it right.
And the second good thing about the question is that it also makes you think about what would have been returned if the case had been correct. I know I was not really sure of that (I'm far from an XML expert!), so I was happy that the lower-/uppercase thing gave me a guaranteed point, and the rest of the question gave me the opportunity to play with the code and see what happens if I change the case - so this has also taught me something about the XQuery data function. (And if I just happen to stumble on it again within a week or so, it might even stick this time).


I agree Hugo, however, the lack of case insensitivity caught me out. It has made me go and refresh my knowledge about XQuery... Not something I use in my normal day-to-day working life...
udayroy15
udayroy15
SSC-Addicted
SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)

Group: General Forum Members
Points: 446 Visits: 101
Great Smile
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search