Re: How to select an attribute value from an XML-column using XPath/XQuery?
- From: "Denis Ruckebusch [MSFT]" <denisruc@xxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 24 Apr 2007 11:00:03 -0700
The problem is that the XPath expression must _statically_ resolve to a
singleton.
In the case of untyped XML an expression like
/Parameters/Parameter[@Name="@SystemUserID"] statically infers
element(Parameter, xdt:untyped)*
The star indicates that we can expect 0 or more of these elements.
In order to infer a singleton you can add a position predicate like this:
(/Parameters/Parameter[@Name="@SystemUserID"])[1]. The static type becomes
element(Parameter, xdt:untyped)?
The question mark indicates that we expect 0 or 1 of these elements. The
singleton requirement is met.
A while ago I addressed this category of problems on my blog. Here's the link:
http://blogs.msdn.com/denisruc/archive/2005/06/14/429055.aspx
I hope this helps
Denis Ruckebusch
--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Robert Pettersson" <robpet@xxxxxxxxxxx> wrote in message
news:%23HgPoakhHHA.3452@xxxxxxxxxxxxxxxxxxxxxxx
Hi,
I have stored some XML data in a XML-column in one of my tables. It looks
something like this:
<Parameters>
<Parameter Name="View" Value="A" />
<Parameter Name="@HistDate" Value="2007-02-12" />
<Parameter Name="@ManagerID" Value="null" />
<Parameter Name="@SystemUserID" Value="2" />
</Parameters>
My XML will always have one Parameter-element with the attribute
Name="@SystemUserID", and I want to be able to select its value.
I have tried, but I dont get it to work.
If I use the following question, I can select the right Parameter element:
Select
Parameters.query('/Parameters/Parameter[@Name="@SystemUserID"]') As
SystemUserIDElement
From
MyTable
Parameter is the name of my XML-Column.
I get this result:
<Parameter Name="@SystemUserID" Value="2" />
That seems right. But I want to select the value of the Value attribute. In
this case "2".
I think that I should be able to do that by running this query:
Select
Parameters.value('/Parameters/Parameter[@Name="@SystemUserID"]/@Value',
'int') As SystemUserIDValue
From
MyTable
But all I get is this error message:
Msg 2389, Level 16, State 1, Line 6
XQuery [MyDB.dbo.MyTable.Parameters.value()]: 'value()' requires a singleton
(or empty sequence), found operand of type 'xdt:untypedAtomic *'
I guess there is a really simple solution to this. But I cant find any good
help on the web. So if anyone could help me with this I would be very
greatful.
If someone has a good link to a site that has information on how to
query/select data from XML Data Columns in Sql Server 2005 that would be very
appreciated.
Thanks,
Robert
.
- References:
- How to select an attribute value from an XML-column using XPath/XQuery?
- From: Robert Pettersson
- How to select an attribute value from an XML-column using XPath/XQuery?
- Prev by Date: Re: How to select an attribute value from an XML-column using XPath/XQuery?
- Next by Date: MSSQL2K FOR XML Explicit help?
- Previous by thread: Re: How to select an attribute value from an XML-column using XPath/XQuery?
- Next by thread: MSSQL2K FOR XML Explicit help?
- Index(es):
Relevant Pages
|