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


Converting XPath to FLWOR


Converting XPath to FLWOR

Author
Message
gbritton1
gbritton1
Say Hey Kid
Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)

Group: General Forum Members
Points: 671 Visits: 864
I'm trying to go deeper into SQL Server's XML support. When looking at a recent forum post, I got curious to see if I could achieve the same result with XPath and FLOWR. So far, I',m stumped.

Here's the data I'm working with:

create table #data (myXMLData xml)
insert into #data(myXMLData) values (
'<?xml version="1.0" encoding="utf-8"?>
<order>
<Product Index="e1e2c499-f9...etc...." ProductID="12" >
<Attribute Name="Paper...etc..." />
<Attribute Name="Ink..etc.." />
<Attribute Name="Bleed ..etc " />
<Drops returnCompany="ACME" returnAddress1="...etc...">
<Drop Number="1" Total="3141" Date="07/06/2014">
<Area Name="43001PBOX" FriendlyName="ALEXANDRIA, OH" Total="165" />
<Area Name="43001R001" FriendlyName="ALEXANDRIA, OH" Total="393" />
<Area Name="43001R002" FriendlyName="ALEXANDRIA, OH" Total="445" />
<Area Name="43023PBOX" FriendlyName="GRANVILLE, OH" Total="236" />
<Area Name="43023R001" FriendlyName="GRANVILLE, OH" Total="666" />
<Area Name="43023R002" FriendlyName="GRANVILLE, OH" Total="497" />
<Area Name="43031PBOX" FriendlyName="JOHNSTOWN, OH" Total="166" />
<Area Name="43031R001" FriendlyName="JOHNSTOWN, OH" Total="573" />
</Drop>
<Drop Number="2" Total="4279" Date="07/27/2014">
<Area Name="43031R002" FriendlyName="JOHNSTOWN, OH" Total="611" />
<Area Name="43031R003" FriendlyName="JOHNSTOWN, OH" Total="582" />
<Area Name="43031R004" FriendlyName="JOHNSTOWN, OH" Total="533" />
<Area Name="43031R005" FriendlyName="JOHNSTOWN, OH" Total="511" />
<Area Name="43055R003" FriendlyName="NEWARK, OH" Total="638" />
<Area Name="43062PBOX" FriendlyName="PATASKALA, OH" Total="361" />
<Area Name="43062R009" FriendlyName="PATASKALA, OH" Total="516" />
<Area Name="43062R017" FriendlyName="PATASKALA, OH" Total="527" />
</Drop>
</Drops>
</Product>
</order>
')



Here's the working XPath query and results:

select data.mynodes.value('@Name', 'varchar(50)') Name
from #data
cross apply myXMLData.nodes('/order/Product/Drops/Drop[@Number="1"]/Area') data(mynodes)



Results:

Name
43001PBOX
43001R001
43001R002
43023PBOX
43023R001
43023R002
43031PBOX
43031R001


Here's my first attempt at a FLWOR expression. Note that doesn't produce the same results.

select myXmlData.query('
for $drop in /order/Product/Drops/Drop
for $Name in $drop/Area/@Name
where $drop/@Number=1
return data($Name)
')
from #data



Results:

43001PBOX 43001R001 43001R002 43023PBOX 43023R001 43023R002 43031PBOX 43031R001


Can anyone help me build a FLOWR-based query that produces the same result as the pure XPath one?
Alan.B
Alan.B
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5322 Visits: 7736

Can anyone help me build a FLOWR-based query that produces the same result as the pure XPath one?


I don't know if that is possible. I thought I saw an article out there where someone replaces that nodes method (which you are using in your first query) with a FLOWR statement but I can't find it. I gave this a shot but failed Laugh

The best I could do for you is this solution:

WITH xml_txt(txt) AS
(
select cast(myXmlData.query
('
for $drop in /order/Product/Drops/Drop[@Number=1]/Area/@Name
return string($drop)
')
AS varchar(100))
from #data d
)
SELECT item
FROM xml_txt
CROSS APPLY dbo.DelimitedSplit8K(txt,' ') s



This uses Jeff Moden's splitter function (referenced in my signature). I tested this with 100,000 XML records in #data, however, and the FLOWR/Splitter solution runs for about 30 seconds whereas your original query runs for 8 seconds. Your query, when doing 1000's of records, produces a parallel query plan; my solution did not so I updated it to include Adam Mechanic's MakeParallel() function which reduced the query time by about 1/3rd but was still slower than the original solution that you posted.

The final solution looked like this:

WITH xml_txt(txt) AS
(
select cast(myXmlData.query
('
for $drop in /order/Product/Drops/Drop[@Number=1]/Area/@Name
return string($drop)
')
AS varchar(100))
from #data d
)
SELECT item
FROM make_parallel()
CROSS APPLY xml_txt
CROSS APPLY dbo.DelimitedSplit8K(txt,' ') s



All that said, you could clean your query up to look like this:

select data.mynodes.value('.', 'varchar(50)') Name
from #data
cross apply myXMLData.nodes('(/order/Product/Drops/Drop[@Number="1"]/Area/@Name)') data(mynodes);



-- Alan Burstein



Best practices for getting help on SQLServerCentral
Need to split a string? Try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL 2012+)
Need a pattern-based splitter? Try PatternSplitCM
Need to remove or replace those unwanted characters? Try PatExclude8K and PatReplace8K.

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001
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