如何向HXT箭头传递参数以及如何使用-<<

发布于 2024-12-06 13:35:55 字数 2554 浏览 1 评论 0 原文

我的问题如下。 我有这个 xml 文件要解析:

<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>

我想要以下输出:

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

但是下面的代码给了我:

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

我只想解析一次 XML 以检索“错误标签”和错误。

我认为我的问题出在函数 errToLab 中,但我没有找到明显的解决方案。

感谢您的帮助。

这是代码

{-# 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

my question is the following.
I have this xml file to parse :

<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>

and I would like to have the following output:

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

But the code below gives me :

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

I would like to parse only once the XML to retrieve the "ERRORS label" and the ERR.

I think my problem is in the function errToLab but no obvious solution comes to me.

thanks for your help.

Here is the 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稳稳的幸福 2024-12-13 13:35:55

只需将错误列表提供给箭头输入即可。

这是一个稍微编辑过的版本:

{-# 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

Just feed Errors list to arrows input.

Here is a slightly edited version:

{-# 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文