For the usage in a SQL query I need to get the value of a specific XML element. The XML element is specified by its attribute.
My XML elements looks like this:
<translations>
<value lang="en-US">example</value>
<value lang="de-DE">Beispiel</value>
</translations>
and the value I am looking for would be "example" when I specify lang to be "en-US".
I found a way to get this value by using the query() function and afterwards the value() function.
declare @S varchar(max)
set @S =
'<translations>
<value lang="en-US">example</value>
<value lang="de-DE">Beispiel</value>
</translations>'
declare @X xml
set @X = CAST(@S as xml)
select @X.query('/translations/value[@lang="en-US"]').value('.','varchar(max)')
This select statement return the value "example" I am looking for by using the query() and value() function. But is there also a - more convenient - way to only use value() OR query()?
Thank you in advance!