I refer to How to get a particular attribute from XML element in SQL Server.
I do not have a particular attribute with the value of the node in it, but rather the I am looking for the value of the node depending on which attribute I am looking for.
I am selecting from an XML column in a table in SQL, but for simplicity sake, I can provide the following example:
DECLARE @xml xml = CONVERT(XML, '<?xml version="1.0" encoding="UTF-8"?>
<alert>
<hits>
<elem name="hit">
<elem name="scoreFactors">
<elem name="scoreFactors">
<elem name="factorId">FactorID 1</elem>
<elem name="factorDesc">FactorDesc 1</elem>
<elem name="factorValue">FactorValue 1</elem>
<elem name="factorScore">FactorScore 1</elem>
<elem name="factorImpact">FactorImpact 1</elem>
</elem>
<elem name="scoreFactors">
<elem name="factorId">FactorID 2</elem>
<elem name="factorDesc">FactorDesc 2</elem>
<elem name="factorValue">FactorValue 2</elem>
<elem name="factorScore">FactorScore 2</elem>
<elem name="factorImpact">FactorImpact 2</elem>
</elem>
</elem>
</elem>
</hits>
</alert>')
SELECT @xml.value('(/alert/hits/elem/elem[@name="factorDesc"])[1]', 'nvarchar(max)')
All I am getting back is a NULL
value, but I am expecting 2 rows with FactorDesc 1
and FactorDesc 2
as values, respectively.
What am I doing wrong?