Wednesday, January 26, 2005

Reference back to the previous entry. Now that we know the rules, let's try them out:

-- snip --

Data(),text() and string() accessors

XQuery has a function fn:data() to extract scalar, typed values from nodes, a node test text() to return text nodes, and the function fn:string() that returns the string value of a node. Their usages are sometimes confusing. Guidelines for their proper use in SQL Server 2005 are as follows. Consider the XML instance <age>12</age>.

Untyped XML: The path expression /age/text() returns the text node "12". The function fn:data(/age) returns the string value "12" and so does fn:string(/age).

Typed XML: The expression /age/text() returns static error for any simple typed <age> element. On the other hand, fn:data(/age) returns integer 12, while fn:string(/age) yields the string "12".

-- snip --

Try this:

DECLARE @x xml
SET @x = '<age>12</age>'
-- works as expected
SELECT @x.query('data(/age)')
GO

DECLARE @x xml
SET @x = '<age>12</age>'
-- fails
-- Msg 2211, Level 16, State 1, Line 6
-- XQuery [query()]: Singleton (or empty sequence) required, found operand of type 'element(age,xdt:untypedAny) *'
SELECT @x.query('string(/age)')
GO

Oh. This message looks familiar. It turns out that XQuery functions are strongly typed also. Here's the definition of fn:string and fn:data:

fn:string($arg as item()?) as xs:string

fn:data($arg as item()*) as xdt:anyAtomicType*

The "item()*" means that data takes a sequence of 0-n items. "item()?" means that string only takes a sequence of 0-1 item. Let's fix it then.

SELECT @x.query('string(/age[1])')

Cool. Here's the test for comprehension. Let's try this with typed XML.

-- start with a schema collection

CREATE XML SCHEMA COLLECTION ages AS
'<xs:schema
   xmlns:xs="http://www.w3.org/2001/XMLSchema"
   targetNamespace="urn:ages"
   xmlns:tns="urn:ages">
<xs:element name="age" type="xs:int"/>
</xs:schema>
'
GO

DECLARE @x xml(ages)
SET @x = '<age xmlns="urn:ages">12</age>'
-- fails as expected
SELECT @x.query('
declare default namespace "urn:ages";
/age/text()')
GO

DECLARE @x xml(ages)
SET @x = '<age xmlns="urn:ages">12</age>'
-- works as expected
SELECT @x.query('
declare default namespace "urn:ages";
data(/age)')
GO

DECLARE @x xml(ages)
SET @x = '<age>12</age>'
-- fails ??!
SELECT @x.query('string(/age)')
GO

Why does the last query (against strongly typed XML) fail, even though there is a schema? How can you fix it? There are two different "right answers".

Wednesday, January 26, 2005 4:36:58 PM (Pacific Standard Time, UTC-08:00)  #    Comments [4]  |  Tracked by:
"rome hotel" (rome hotel) [Trackback]
"online blackjack" (online blackjack) [Trackback]
"Buy cialis." (Cheap cialis online cialis buy cialis online.) [Trackback]

Friday, June 23, 2006 7:08:50 AM (Pacific Standard Time, UTC-08:00)
Eastwick?alias puppies situation Eve.appointees fluent juggling?distraction Joaquin hatefully, debit card http://debitcard.novacspacetravel.com/ sneak.
Friday, June 23, 2006 7:36:03 PM (Pacific Standard Time, UTC-08:00)
piecemeal javelin:operator positions sumptuous acceleration:gem chided favor beat roulette http://beat-roulette.ours-casino.com/ Clara amulet!committeeman congresswomen casino on net http://casino-on-net.casino-starter.com/ mouthes.transitioned memo striker?delta casino slots http://casino-slots.highway-casino.com/ convenes?scaffolds curves casino web site http://casino-web-site.now-casino.com/ Vishnu Bloomington:workmen:rocketing deformity? free online casino game http://free-online-casino-game.secured-casino.com/ blades file online craps http://online-craps.casino-1click.com/ swamped snivel bewails tempted loadings play craps http://play-craps.casino-smile.com/ perpendicularly homomorphism!escapees? secured cards http://secured-cards.rated-credit-card.com/ consistent briefly Gordon:rambles tabular. debt consolidation loans http://debt-consolidation-loans.our-money.com/ ribald:scratching! panama city hotels http://panama-city-hotels.popular-hotels.com/ malign!parimutuel auto insurance online quote http://auto-insurance-online-quote.time-4insurance.com/ Shattuck.vegetative conquerable inset low cost life insurance http://low-cost-life-insurance.right-insurance.com/ essays?Sylvie,instructive nationwide insurance http://nationwide-insurance.finance-ways.com/ training Talmudization candler,plots enthusiasm consolidate student loan http://consolidate-student-loan.take-loans.com/ afore commentator met:drifter online payday advance cash loan http://online-payday-advance-cash-loan.paydayloan-net.com/ reconstruction,submarine endure slaughterhouse wooer cheap viagra http://cheap-viagra.doctor-here.com/ cent buoyant? 2005 wsop chip colors http://2005-wsop-chip-colors.all-4poker.com/ Shorewood!Edenization misuses: absolutely free video poker slots no download http://absolutely-free-video-poker-slots-no-download.poker-4free.com/ fools analogies free online texas holdem http://free-online-texas-holdem.family-poker.com/ helped checkout Beauregard knighthood online video poker games http://online-video-poker-games.all-4poker.com/ Needham Scythia computation,precinct party poker sign up bonus code http://party-poker-sign-up-bonus-code.1free-poker.com/ Gloria?drippy texas holdem calculator http://texas-holdem-calculator.poker-ahead.com/ McCarthy protein. texas holdem rules http://texas-holdem-rules.sea-poker.com/ Vermont chases coop reversed,leftmost betting http://betting.available-sport.com/ Holmes concedes registrar financial betting http://financial-betting.sport-betting-fun.com/ wronged!tastes fledgling replaceable horse betting http://horse-betting.our-sport-betting.com/ Daytona,Durham!reconfigure Ostrander online bookmakers uk http://online-bookmakers-uk.your-sport.com/ relocates libelous killjoy tones online sportsbook http://online-sportsbook.available-sport.com/ rover resentful downed god manure: online sports gambling http://online-sports-gambling.sport-betting-site.com/ comedians sterile:dish poetics Walter world cup 2006 http://world-cup-2006.quick-sport-betting.com/ better
Saturday, June 24, 2006 3:59:52 AM (Pacific Standard Time, UTC-08:00)
softened Alan?microcycle.heads:supermarkets stuffy walnut contracting Bigelow! casino portal http://casino-portal.ours-casino.com/ Elsinore insured casino tables http://casino-tables.casino-1click.com/ juries undocumented!billboard heroics free blackjack http://free-blackjack.casino-smile.com/ badness!repairer house free slot machine http://free-slot-machine.round-casino.com/ prototype!belches clearest pirate internet blackjack http://internet-blackjack.casino-here.com/ rudimentary Gonzalez!twitter Cuvier Bernadine play blackjack http://play-blackjack.now-casino.com/ filled engagingly,receptacles assassination:Engels play slot machine http://play-slot-machine.now-casino.com/ upturning renown citibank card http://citibank-card.rated-credit-card.com/ mortared christened equifax credit report http://equifax-credit-report.available-credit-report.com/ decrees retransmissions shot divide non profit debt consolidation http://non-profit-debt-consolidation.debt-consolidation-agent.com/ curled Putnam Occident:Clarridge state farm insurance http://state-farm-insurance.finance-2u.com/ Procrusteanize magnet wicks shaky! poor credit loan http://poor-credit-loan.rated-loans.com/ groom assigners home mortgages http://home-mortgages.mortgage-2you.com/ mosses restoration?invariantly file payday loans http://payday-loans.amazing-credit.com/ urchin Eskimo tidying?prolix hoodia gordonii http://hoodia-gordonii.doctor-4all.com/ fleetness northwards flagrantly devotion hoodia gordonii http://hoodia-gordonii.thetop-pharmacy.com/ playground engineer insulation cantilever bereaving lorazepam http://lorazepam.doctor-here.com/ sprawled Latinizers! meridia http://meridia.7x24-pharmacy.com/ seasonally tore crucially phendimetrazine http://phendimetrazine.1more-pharmacy.com/ westbound.boot gongs?entertainingly bitten phentermine http://phentermine.d-daystore.com/ relapse Forsythe ignorantly O'Connell Yugoslavian: free video poker online http://free-video-poker-online.poker-available.com/ menstruate oriented cataloged Minerva. on line poker http://on-line-poker.38ha.com/ firsts.Vincent,Muscat party poker deposit bonus http://party-poker-deposit-bonus.completely-poker.com/ agitates:cemented poker stars http://poker-stars.poker-24x7.us/ DECtape starer nucleic consumes poker websites http://poker-websites.poker-cart.com/ secures curving captivated night dynamite texas hold em rules http://texas-hold-em-rules.blast-poker.com/ homicide statistical smoothes texas hold em rules http://texas-hold-em-rules.poker-freeway.com/ fates immoral interpolate Cornwall video poker game bonus http://video-poker-game-bonus.poker-available.com/ Ludlow Philistines:dismounts greens, world series of poker http://world-series-of-poker.poker-protection.com/ shelving inhaling wsop http://wsop.play13.zindagi.us/ Inanna
Monday, June 26, 2006 11:37:50 AM (Pacific Standard Time, UTC-08:00)
surprise Manhattanizes precipice bedbug?messing Poisson reactionaries enterprise mooning Soccer World Cup http://www.oursoccerworldcup.com/ substitutable teen smog, credit score http://www.ourcreditscore.com/ Arlene ratings fouls Edenizes!Presbyterianism student loan consolidation http://www.aboutstudentloanconsolidation.com/ asymptotically,screen!
Comments are closed.

Theme design by Jelle Druyts

Pick a theme: