2011-09-26 1 views
1

Ma question est la suivante. J'ai ce fichier xml pour analyser:Comment passer les paramètres aux flèches HXT et comment utiliser - <<

<DATAS LANG="en"> 
<SCENARIO ID="19864"> 
    <ORIGIN ID="329"> 
     <SCENARIO_S ERR="0"></SCENARIO_S> 
     <SCENARIO_S ERR="2"></SCENARIO_S> 
    </ORIGIN> 
</SCENARIO> 
<ERRORS> 
    <ERROR ID="0" LABEL="Aggregated Major Errors" /> 
    <ERROR ID="2" LABEL="Banner error" /> 
</ERRORS> 
</DATAS> 

et je voudrais avoir la sortie suivante:

[("19864","329",[0,2], ["Aggregated Major Errors", "Banner error"])] 
that is 
[(Scenario ID, Origin ID, [ERR],[Errors label])] 

Mais le code ci-dessous me donne:

[("19864","329",[0,2],["","*** Exception: Maybe.fromJust: Nothing 

Je voudrais analyser seulement une fois le XML pour récupérer le "label ERRORS" et le ERR.

Je pense que mon problème est dans la fonction errToLab mais aucune solution évidente ne vient à moi.

merci pour votre aide.

Voici le code

{-# LANGUAGE Arrows, NoMonomorphismRestriction #-} 
import Text.XML.HXT.Core 
import Data.Maybe 

dataURL = "test.xml" 

parseXML file = readDocument [ withValidate no 
         , withRemoveWS yes -- throw away formating WS 
         ] file 

atTag tag = deep (isElem >>> hasName tag) 

getErrLab2 = atTag "ERRORS" >>> 
    proc l -> do 
    error <- atTag "ERROR"   -< l 
    errID <- getAttrValue "ID"  -< error 
    desc <- getAttrValue "LABEL"  -< error 
    returnA -< (errID,desc) 

getErr = atTag "SCENARIO_S" >>> 
    proc p -> do 
    err <- getAttrValue "ERR" -< p 
    returnA -< read err::Int 

getScenar2' errlab = atTag "SCENARIO" >>> 
    proc l -> do 
    scenarTag <- atTag "SCENARIO"  -< l 
    scenName <- getAttrValue "ID"  -< l 
    site  <- atTag "ORIGIN"   -< l 
    siteName <- getAttrValue "ID"  -< site 
    errs  <- listA getErr   -< site 
    errlab <- listA (errToLab errlab) -< site 
    returnA -< (scenName,siteName,errs,errlab) 

getData= atTag "DATAS" >>> 
    proc p -> do 
      errlab <- getErrLab2 -< p 
      datascen <- getScenar2' [errlab] -<< p 
      returnA -< datascen 

errToLab errlab = atTag "SCENARIO_S" >>> 
    proc p -> do 
      err <- getAttrValue "ERR" -< p 
      returnA -< chercheErr err errlab 

    where 
      chercheErr "0" _ = "" 
      chercheErr err taberr = fromJust.lookup err $ taberr 

main = do 
    site <- runX (parseXML dataURL >>> getData) 
    print site 
+0

Je ne suis pas HXT (ou flèche) utilisateur, mais certains instruments simples montre que 'taberr' n'a que l'identificateur d'erreur lorsque vous 0'' essayez de numéro d'erreur de recherche '2'. Il semble être traité de manière incrémentale et entrelacé, lorsque vous voulez que la table d'erreurs entière soit chargée en premier. –

+0

On dirait que personne ne comprend les flèches, y compris moi;) – alternative

+0

Merci à votre commentaire, je pourrais trouver où mon erreur était. Dans la fonction getData, la ligne 'errlab <- getErrLab2 -

Répondre

2

se nourrissent juste Liste des erreurs aux flèches d'entrée.

Voici une version légèrement modifiée:

{-# LANGUAGE Arrows #-} 
import Text.XML.HXT.Core 
import Data.Maybe 

dataURL = "test.xml" 

parseXML file = readDocument [ withValidate no 
          , withRemoveWS yes -- throw away formating WS 
          ] file 

atTag tag = deep (isElem >>> hasName tag) 

getErrLab2 = atTag "ERRORS" >>> 
    proc l -> do 
    error <- atTag "ERROR"  -< l 
    errID <- getAttrValue "ID" -< error 
    desc <- getAttrValue "LABEL" -< error 
    returnA -< (errID,desc) 

getErr = atTag "SCENARIO_S" >>> 
    proc p -> do 
    err <- getAttrValue "ERR" -< p 
    returnA -< read err::Int 

getScenar2' = proc (p,errlab) -> do 
    l <- atTag "SCENARIO" -< p 
    scenarTag <- atTag "SCENARIO" -< l 
    scenName <- getAttrValue "ID" -< l 
    site  <- atTag "ORIGIN" -< l 
    siteName <- getAttrValue "ID" -< site 
    errs  <- listA getErr  -< site 
    elab  <- listA errToLab -< (site,errlab) 
    returnA -< (scenName,siteName,errs,elab) 

getData= atTag "DATAS" >>> 
    proc p -> do 
     errlab <- listA getErrLab2 -< p 
     getScenar2' -< (p, errlab) 

errToLab = proc (s,errlab) -> do 
    p <- atTag "SCENARIO_S" -< s 
    err <- getAttrValue "ERR" -< p 
    returnA -< chercheErr err errlab 

    where 
     -- chercheErr "0" _ = "" 
     chercheErr err taberr = fromJust.lookup err $ taberr 

main = do 
    site <- runX (parseXML dataURL >>> getData) 
    print site 
Questions connexes