Trying to return the value of an XML field via Xpath query.
Here is what the XML looks like in a snap shot.
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfCustomProperty xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CustomProperty>
<DeveloperId>Test123</DeveloperId>
<Key>AgreedToTerms</Key>
<Value>True</Value>
</CustomProperty>
<CustomProperty>
<DeveloperId>Test456</DeveloperId>
<Key>ValidForLoyaltyPoints</Key>
<Value>False</Value>
</CustomProperty>
</ArrayOfCustomProperty>
(sorry for the poorly-formatted XML...StackOverflows RCE is having issues with rendering it out. The ArrayOfCustomProperty is closed at the end, just not displayed for some reason)
This query works for me....
SET @return = CAST(CAST(@xmlData AS xml).query('ArrayOfCustomProperty/CustomProperty/Key[text()=sql:variable("@key")]/../Value/text()') AS nvarchar(255))
It allows me to have a function where the parameters are @xmlData and @key for what needs to be searched. I need to have another function (or might modify this one) where I can also search the [DeveloperId] node as well, so a third parameter will be passed in as @devId. I've tried quite a few different things but nothing has worked for me yet. I'd like a query where I can get the [Value] when [DeveloperId] and [Key] are present using the same structure (if possible) to how the current Xpath query works.
Thanks in advance for any help.