Re: OPENXML to accept both attributes and elements

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Goran Djuranovic wrote:

Is it possible to retreive values from XML document whether they where sent as elements or attributes, using OPENXML?
For example, if someone sends me:

<Root><Customer Name="blah"></Customer></Root> or
<Root><Customer><Name>blah</Name></Customer></Root>

I want to be able to read both. Possible?

I am not sure about OPENXML but XQuery can do it:

DECLARE @doc XML;
SET @doc = N'<Root><Customer Name="Foo"></Customer><Customer><Name>Bar</Name></Customer></Root>';

SELECT
T.c.value('(@Name, Name)[1]', 'nvarchar(20)') AS [Name]
FROM
@doc.nodes('Root/Customer') AS T(c);



--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
.