I have XML in the following format:
<filters>
<filter name="filterByOperatingSystem">
<parameters operatingSystem="Windows" />
</filter>
<filter name="filterBySoftware">
<parameter software="Office" />
</filter>
</filters>
stored in @XML.
What I would like to do is loop though each of the filters so that I can do some processing.
My thought was that I could grab each filter name along with the parameters element in a cursor, but the closest I can get so far is:
DECLARE crsDTO cursor static forward_only read_only for
SELECT
tab.col.value('@name','NVARCHAR(64)')
FROM
@XML.nodes('//filter') tab(col)
I tried the following query to see if I could get the parameter xml out:
DECLARE crsDTO cursor static forward_only read_only for
SELECT
tab.col.value('@name','NVARCHAR(64)'),
tab.col.value('parameter[1]' 'XML')
FROM
@XML.nodes('//filter') tab(col)
But I got the error:
The datatype XML used in the value type is invalid
Because my filters can have different attributes I do not want to attempt to grab the attributes directly with the first query (my thought was to do the cursor, then depending on the filter name, I could get the specific parameters from the node), but without being able to get the xml node out, i’m not sure how to go about doing this.
Any suggestions as to how I could go about resolving this?
Do you need this?
if it’s possible that filter has other child element types you might want to change
to
or
what ever the right name is.