繁体   English   中英

HXT使用“ orElse”将缺少的属性值替换为默认值

[英]HXT using 'orElse' to replace missing attribute value with default

我正在使用HXT解析一个简单的XML文件,并且需要用默认值替换缺少的标签属性。 但是由于某种原因, orElse无法按预期工作。

下面是XML文件:

<!-- window_home.xml -->
<elements>
  <Window libraryItemName="panel_tabs" name="panel_tabs" selected="true">
    <matrix>
      <Matrix ty="-11.8" />
    </matrix>
  </Window>
  <Window libraryItemName="home_powerup_menu" name="home_powerup_menu" selected="true">
    <matrix>
      <Matrix tx="12.4" />
    </matrix>
  </Window>
  <Window libraryItemName="panel_name" name="panel_name" selected="true">
    <!-- data here  -->
  </Window>
</elements>

有问题的标签是Matrix 下面是我的代码:

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

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

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

data XflMatrix = XflMatrix { a, b, c, d, tx, ty :: Float } deriving (Show)
initXflMatrix = XflMatrix { a = 1.0, d = 1.0, b = 0.0, c = 0.0, tx = 0.0, ty = 0.0 }

data UiWindow = UiWindow {
    wndName :: String,
    wndNameLib :: String,
    wndMatrix :: XflMatrix,
    wndChildren :: [UiWindow]
    } deriving (Show)

initUiWindow = UiWindow {
    wndName = "empty",
    wndNameLib = "",
    wndMatrix = initXflMatrix,
    wndChildren = []
    }


parseDoc docName = runX $ parseXML fileName >>> getWindow
  where
    fileName = docName ++ ".xml"

getMatrixFromTag = atTag "Matrix" >>> proc x -> do
    tx <- getFloatAttrib "tx" -< x
    ty <- getFloatAttrib "ty" -< x
    returnA -< initXflMatrix { tx = read tx :: Float, ty = read ty :: Float }
        where
            --getFloatAttrib attribName = getAttrValue attribName
            getFloatAttrib attribName = getAttrValue attribName `orElse` constA "0.0"

getWindow = atTag "Window" >>> proc x -> do
    libraryItemName <- getAttrValue "libraryItemName" -< x
    name <- getAttrValue "name" -< x
    m <- getMatrixFromTag -< x
    children <- arrIO parseDoc -< libraryItemName
    returnA -< initUiWindow { wndName = name, wndNameLib = libraryItemName, wndChildren = children, wndMatrix = m}


documentName = "DOMDocument.xml"        

parseRoot = parseXML documentName
--runX (parseRoot >>> getWindow )

看起来像线

getFloatAttrib attribName = getAttrValue attribName `orElse` constA "0.0"

不返回"0.0"

难道我做错了什么?

好吧,那太愚蠢了。 我应该检查一下类型:

λ: :t getAttrValue
getAttrValue :: ArrowXml a => String -> a XmlTree String
λ: :t hasAttr
hasAttr :: ArrowXml a => String -> a XmlTree XmlTree

现在,将相关行更改为:

getFloatAttrib attribName = (hasAttr attribName >>> getAttrValue attribName) `orElse` constA "0.0"

一切顺利。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM