Je tente de générer un fichier xml d'une base de données de recettes contenant des ingrédients avec des sous-éléments. Ma requête est la suivante:Problème avec l'imbrication correcte à l'aide de SQL Server FOR XML EXPLICIT
select
1 as 'Tag'
,null as 'parent'
,replace(r.recipe_name, '/', '') as 'item!1!title!element'
,isnull(replace(r.description, '/', ''), '') as 'item!1!description!cdata'
,r.recipe_id as 'item!1!recipe_id!element'
,null as 'ingredients!2!ingredient!element'
from recipe r
union all
select
2 as 'Tag'
,1 as 'parent'
,null as 'item!1!title!element'
,null as 'item!1!description!cdata'
,r.recipe_id as 'item!1!recipe_id!element'
,i.full_ingredient_txt as 'ingredients!2!ingredient!element'
from
recipe r, ingredient i
where r.recipe_id = i.recipe_id
order by 'item!1!recipe_id!element'
for xml explicit
qui génère le code XML suivant:
<item>
<title>3-D Cookie Packages</title>
<description><![CDATA[]]></description>
<recipe_id>52576</recipe_id>
<ingredients>
<ingredient>Assorted candy decorations, if desired</ingredient>
</ingredients>
<ingredients>
<ingredient>cup butter or margarine, softened</ingredient>
</ingredients>
<ingredients>
<ingredient>cup sugar</ingredient>
</ingredients>
</item>
Ce que je veux vraiment est mes ingrédients pour nicher comme ceci:
<ingredients>
<ingredient>Assorted candy decorations, if desired</ingredient>
<ingredient>cup butter or margarine, softened</ingredient>
<ingredient>cup sugar</ingredient>
</ingredients>
Je ne peux pas utiliser FOR XML PATH car j'ai besoin de la déclaration CDATA dans le champ description, qui n'est pas supporté avec cette méthode.
fonctionne comme un charme. Merci. Le seul changement que j'ai dû faire était d'ajouter une instruction select après la première déclaration d'union. –
Juste attrapé que .. – Rich