2017-09-11 1 views
2

est-il un moyen d'analyser le document XML suivant dans PostgreSQL en utilisant XPath pour obtenir la sortie désirée commeAnalyse de document XML dans PostgreSQL en utilisant XPATH?

--Corrected sortie requise

promotion-id price new-promotion null new-promotion null new-promotion 300

pour l'élément price. Actuellement, lorsque je cours la requête fournie ci-dessous, je reçois 300 comme sortie. Mon problème est que ma requête ignore les éléments de prix ayant la structure <price xsi:nil="true"/>.

Existe-t-il un moyen de renvoyer null comme résultat pour les éléments de prix avec structure de type <price xsi:nil="true"/>?

Mon code est quelque chose comme:

WITH x AS (SELECT 
'<promotions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <promotion promotion-id="old-promotion"> 
     <enabled-flag>true</enabled-flag> 
     <searchable-flag>false</searchable-flag> 
    </promotion> 
    <promotion promotion-id="new-promotion"> 
     <enabled-flag>false</enabled-flag> 
     <searchable-flag>false</searchable-flag> 
     <exclusivity>no</exclusivity> 
     <price xsi:nil="true"/> 
     <price xsi:nil="true"/> 
     <price>300</price> 
    </promotion> 
</promotions>'::xml AS t 
) 
SELECT unnest(xpath('//price/text()', node))::text 
FROM (SELECT unnest(xpath('/promotions/promotion', t)) AS node FROM x) sub 

Toute suggestion pour la question ci-dessus est très appréciée.

Répondre

1

Le problème est qu'il n'y a pas de texte à extraire des deux premiers éléments de prix. See discussion. Postgresql est actuellement limité à XPath 1.0. Nous devons donc dissocier les fragments XML et les convertir individuellement en texte.

WITH x AS (SELECT 
'<promotions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <promotion promotion-id="old-promotion"> 
     <enabled-flag>true</enabled-flag> 
     <searchable-flag>false</searchable-flag> 
    </promotion> 
    <promotion promotion-id="new-promotion"> 
     <enabled-flag>false</enabled-flag> 
     <searchable-flag>false</searchable-flag> 
     <exclusivity>no</exclusivity> 
     <price xsi:nil="true"/> 
     <price xsi:nil="true"/> 
     <price>300</price> 
    </promotion> 
</promotions>'::xml AS t 
) 
SELECT (xpath('@promotion-id',node))[1]::text AS "promotion-id", 
     (xpath('/price/text()', 
       unnest(xpath('price',node)) 
     ))[1]::text AS price 
    FROM (SELECT unnest(xpath('/promotions/promotion', t)) AS node FROM x) sub 
+0

merci pour le message. la solution proposée aurait fonctionné mais la sortie désirée nécessite une référence à l'élément pormotion-id. S'il vous plaît voir le message mis à jour. Apprécier ton aide. –