Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

TSQL- XQuery Expand / Collapse
Author
Message
Posted Monday, July 29, 2013 3:46 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, December 16, 2014 3:12 AM
Points: 800, Visits: 260
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".
Post #1478504
Posted Monday, July 29, 2013 3:54 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, December 18, 2014 10:22 AM
Points: 1,830, Visits: 6,646
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.
Post #1478506
Posted Monday, July 29, 2013 4:14 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 4:21 PM
Points: 7,928, Visits: 9,653


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
Post #1478513
Posted Monday, July 29, 2013 4:21 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 4:21 PM
Points: 7,928, Visits: 9,653
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 .)

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
Post #1478517
Posted Monday, July 29, 2013 4:26 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 3:35 AM
Points: 6,133, Visits: 8,397
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
Post #1478518
Posted Monday, July 29, 2013 4:41 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 4:21 PM
Points: 7,928, Visits: 9,653
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
Post #1478524
Posted Monday, July 29, 2013 9:53 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 12:50 PM
Points: 2,500, Visits: 1,586
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!
Post #1478658
Posted Monday, July 29, 2013 10:02 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Friday, October 24, 2014 12:43 PM
Points: 4,126, Visits: 3,428
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!
Post #1478660
Posted Tuesday, July 30, 2013 2:17 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Thursday, August 8, 2013 6:33 AM
Points: 989, Visits: 16
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...
Post #1478874
Posted Tuesday, July 30, 2013 2:53 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, July 16, 2014 6:22 AM
Points: 271, Visits: 96
Great :)
Post #1478881
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse