dbTalk Databases Forums  

Select multiple detail attributes from xml parameter

microsoft.public.sqlserver.xml microsoft.public.sqlserver.xml


Discuss Select multiple detail attributes from xml parameter in the microsoft.public.sqlserver.xml forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
chris
 
Posts: n/a

Default Select multiple detail attributes from xml parameter - 12-17-2009 , 08:52 AM






Hi -

I'm trying to parse the google analytics xml file returned from my
data query. Under the item node, there are two tags - dxp:dimension
and dxp:metric that I need to extract the value from. When I try my
query, I get "XQuery [value()]: 'value()' requires a singleton (or
empty sequence), found operand of type 'xdt:untypedAtomic *'". I need
to find a way around this but nothing yet. A minimal example follows.

Thanks.

chris

DECLARE @x xml
SET @x = '
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlnspenSearch="http://
a9.com/-/spec/opensearchrss/1.0/" version="2.0">
<channel>
<item>
<guid isPermaLink="false">http://www.google.com/analytics/feeds/
data?ids=xxx&amp;gaagePath=/some/page/path</guid>
<atom:updated>2009-12-15T16:00:00.001-08:00</atom:updated>
<title>gaagePath=/some/path/here</title>
<link>http://www.google.com/analytics</link>
<dxp:dimension xmlns:dxp="http://schemas.google.com/analytics/
2009" name="gaagePath" value="/path/is/interesting"/>
<dxp:metric xmlns:dxp="http://schemas.google.com/analytics/2009"
confidenceInterval="0.0" name="gaageviews" type="integer"
value="533"/>
</item>
</channel>
</rss>'

-- DOESN'T WORK
SELECT
ga.val.value('
declare namespace dxp="http://schemas.google.com/analytics/2009";
dxp:dimension[@value]', 'nvarchar(max)') AS [PagePath]
, ga.val.value('
declare namespace dxp="http://schemas.google.com/analytics/2009";
dxp:metric[@value]', 'nvarchar(max)') AS [Count]
FROM
@x.nodes('/rss/channel/item') AS ga(val)


-- WORKS but I need both values
-- SELECT
-- ga.val.value('@value', 'nvarchar(max)') AS [PagePath]
-- FROM
-- @x.nodes('declare namespace dxp="http://schemas.google.com/
analytics/2009"; /rss/channel/item/dxp:dimension') AS ga(val)

Reply With Quote
  #2  
Old   
chris
 
Posts: n/a

Default Re: Select multiple detail attributes from xml parameter - 12-17-2009 , 09:20 AM






On Dec 17, 9:52*am, chris <chris.cio... (AT) gmail (DOT) com> wrote:
Quote:
Hi -

I'm trying to parse the google analytics xml file returned from my
data query. *Under the item node, there are two tags - dxp:dimension
and dxp:metric that I need to extract the value from. *When I try my
query, I get "XQuery [value()]: 'value()' requires a singleton (or
empty sequence), found operand of type 'xdt:untypedAtomic *'". *I need
to find a way around this but nothing yet. *A minimal example follows.

Reply to my own message. Thanks for Bob in the "parse xml" thread, I
got this to work:
SELECT
root.x.value('declare namespace dxp="http://schemas.google.com/
analytics/2009"; (dxp:dimension/@value, dxp:metric/@value)[1]',
'NVARCHAR(255)') AS a
, root.x.value('declare namespace dxp="http://schemas.google.com/
analytics/2009"; (dxp:dimension/@value, dxp:metric/@value)[2]',
'NVARCHAR(255)') AS b
FROM
@x.nodes('rss/channel/item') root(x)

Thanks Bob.

Reply With Quote
  #3  
Old   
Bob
 
Posts: n/a

Default Re: Select multiple detail attributes from xml parameter - 12-18-2009 , 05:46 AM



Another way to do that:

;WITH XMLNAMESPACES( 'http://schemas.google.com/analytics/2009' AS dxp )
SELECT
root.x.value('(dxp:dimension/@value)[1]','NVARCHAR(255)') AS a,
root.x.value('(dxp:metric/@value)[1]','NVARCHAR(255)') AS b
FROM @x.nodes('rss/channel/item') root(x)


I prefer WITH XMLNAMESPACES which saves you having to repeat the namespace
in queries like this. Also, not sure why you're using a sequence expression,
eg

(dxp:dimension/@value, dxp:metric/@value)[1]

This means, "if dimension/@value is not there, get metric/@value". Is that
what you wanted to do? I've simplified that bit in my query.


Start with this great article:
Introduction to XQuery in SQL Server 2005
http://msdn.microsoft.com/en-us/libr...2(SQL.90).aspx

HTH
wBob

"chris" wrote:

Quote:
On Dec 17, 9:52 am, chris <chris.cio... (AT) gmail (DOT) com> wrote:
Hi -

I'm trying to parse the google analytics xml file returned from my
data query. Under the item node, there are two tags - dxp:dimension
and dxp:metric that I need to extract the value from. When I try my
query, I get "XQuery [value()]: 'value()' requires a singleton (or
empty sequence), found operand of type 'xdt:untypedAtomic *'". I need
to find a way around this but nothing yet. A minimal example follows.


Reply to my own message. Thanks for Bob in the "parse xml" thread, I
got this to work:
SELECT
root.x.value('declare namespace dxp="http://schemas.google.com/
analytics/2009"; (dxp:dimension/@value, dxp:metric/@value)[1]',
'NVARCHAR(255)') AS a
, root.x.value('declare namespace dxp="http://schemas.google.com/
analytics/2009"; (dxp:dimension/@value, dxp:metric/@value)[2]',
'NVARCHAR(255)') AS b
FROM
@x.nodes('rss/channel/item') root(x)

Thanks Bob.
.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.