declare @xml xml=\'
ABC
Dai
Change your query like this:
SELECT STUFF(
(
SELECT '!' + STUFF(p.query(N'for $n in .//*[local-name()!="dailyfrequency"]
return {concat("$",($n/text())[1])}'
).value(N'.',N'nvarchar(max)'),1,1,'')
FROM p.nodes(N'prescription') AS A(p)
FOR XML PATH(''),TYPE).value(N'.',N'nvarchar(max)'),1,1,'')
FROM @xml.nodes(N'/plan/prescriptions') AS A(p);
First we create a derived table for the different prescriptions, Secondly we use the former code for each prescription separately.