Xml 以与HXT一致的方式解析外部文档和当前元素
更新:我现在已经解决了我的主要问题,因此我将奖励我的解决方案是否符合风格的优秀评审 最近我一直在尝试解析文件,这些文件是描述地图的XML文件。该格式中一个有趣的地方是可以指定外部瓷砖集 因为它已经处理了大部分工作,所以我一直在尝试扩展该库,以便它处理外部tileset,但迄今为止没有成功 基本上,我在这里要完成的任务是,给定两个文档,Xml 以与HXT一致的方式解析外部文档和当前元素,xml,haskell,io,arrows,Xml,Haskell,Io,Arrows,更新:我现在已经解决了我的主要问题,因此我将奖励我的解决方案是否符合风格的优秀评审 最近我一直在尝试解析文件,这些文件是描述地图的XML文件。该格式中一个有趣的地方是可以指定外部瓷砖集 因为它已经处理了大部分工作,所以我一直在尝试扩展该库,以便它处理外部tileset,但迄今为止没有成功 基本上,我在这里要完成的任务是,给定两个文档,map.tmx: <?xml version="1.0" encoding="UTF-8"?> <map version="1.0" orient
map.tmx
:
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="12" height="12" tilewidth="64" tileheight="64">
<tileset firstgid="1" source="ground.tsx"/>
...
</map>
返回结构:
Map {..., tilesets = [Tileset { name = "ground", ...}]}
解析(仅限内部)TileSet的工作方法是:
tilesets ∷ IOSArrow XmlTree [Tileset]
tilesets = listA tileset
tileset ∷ IOSArrow XmlTree Tileset
tileset = isElem >>> hasName "tileset" >>> proc ts → do
tsName ← getAttrValue "name" ⤙ ts
tsInitialGid ← getAttrR "firstgid" ⤙ ts
tsTileWidth ← getAttrR "tilewidth" ⤙ ts
tsTileHeight ← getAttrR "tileheight" ⤙ ts
tsMargin ← arr (fromMaybe 0) . getAttrMaybe "margin" ⤙ ts
tsSpacing ← arr (fromMaybe 0) . getAttrMaybe "spacing" ⤙ ts
tsImages ← images ⤙ ts
tsTileProperties ← listA tileProperties ⤙ ts
returnA ⤙ Tileset {..}
where tileProperties ∷ IOSArrow XmlTree (Word32, Properties)
tileProperties = getChildren >>> isElem >>> hasName "tile"
>>> getAttrR "id" &&& properties
images = listA (getChildren >>> image)
我已尝试调整tilesets
方法,使其根据当前元素的source
属性使用当前元素或外部文档,但无效:
tilesets ∷ IOSArrow XmlTree [Tileset]
tilesets = listA $ proc ts → do
source ← isElem >>> hasName "tileset" >>> getAttrValue "source" ⤙ ts
case source of
"" → tileset ⤙ ts
f → tileset ⤙ readDocument [withValidate no, withWarnings yes] f
(这是我多次尝试中的一次)
通常情况下,GHC会告诉我没有使用箭头命令,或者我的值在箭头内,而它不应该在箭头内。我知道我不能以类型安全的方式透明地执行IO(也可能是XHT中间操作),但我被困在这里了。我真的不确定如何继续。我让它与
readFromDocument
和ifA
tilesets ∷ FilePath → IOSArrow XmlTree [Tileset]
tilesets mapPath =
listA $ getChildren >>> isElem >>> hasName "tileset"
>>> getAttrR "firstgid" &&& ifA (hasAttr "source") (externalTileset mapPath) id
>>> tileset
externalTileset ∷ FilePath → IOSArrow XmlTree XmlTree
externalTileset mapPath =
arr (const (dropFileName mapPath)) &&& getAttrValue "source"
>>> arr (uncurry (</>))
>>> readFromDocument [ withValidate no, withWarnings yes ]
>>> getChildren >>> isElem >>> hasName "tileset"
tileset ∷ IOSArrow (Word32, XmlTree) Tileset
tileset = proc (tsInitialGid, ts) → do
tsName ← getAttrValue "name" ⤙ ts
tsTileWidth ← getAttrR "tilewidth" ⤙ ts
tsTileHeight ← getAttrR "tileheight" ⤙ ts
tsMargin ← arr (fromMaybe 0) . getAttrMaybe "margin" ⤙ ts
tsSpacing ← arr (fromMaybe 0) . getAttrMaybe "spacing" ⤙ ts
tsImages ← images ⤙ ts
tsTileProperties ← listA tileProperties ⤙ ts
returnA ⤙ Tileset {..}
where tileProperties ∷ IOSArrow XmlTree (Word32, Properties)
tileProperties = getChildren >>> isElem >>> hasName "tile"
>>> getAttrR "id" &&& properties
images = listA (getChildren >>> image)
tileset∷ 文件路径→ IOSArrow XmlTree[Tileset]
tilesets映射路径=
listA$getChildren>>>isElem>>>具有名称“tileset”
>>>getattr“firstgid”和&ifA(hasAttr“source”)(externalTileset映射路径)id
>>>蒂莱塞特
外部TILESET∷ 文件路径→ IOSROW XmlTree XmlTree
外部TILESET映射路径=
arr(const(dropFileName映射路径))&&&getAttrValue“源”
>>>arr(uncurry())
>>>readFromDocument[带验证否,带警告是]
>>>getChildren>>>>isElem>>具有名称“tileset”
蒂莱塞特∷ IOSArrow(Word32,XmlTree)Tileset
tileset=proc(初始ID,ts)→ 做
tsName← getAttrValue“名称”⤙ ts
蒂莱维特← getAttrR“tilewidth”⤙ ts
tsTileHeight← getAttrR“tileheight”⤙ ts
tsMargin← arr(从0开始)。“利润”⤙ ts
TSP间距← arr(从0开始)。“间距”⤙ ts
tsImages← 图像⤙ ts
tsTileProperties← listA tileProperties⤙ ts
海归⤙ Tileset{..}
tileProperties在哪里∷ IOSArrow XmlTree(Word32,属性)
tileProperties=getChildren>>>isElem>>>具有名称“tile”
>>>getAttrR“id”&&&properties
images=listA(getChildren>>>image)
tilesets ∷ FilePath → IOSArrow XmlTree [Tileset]
tilesets mapPath =
listA $ getChildren >>> isElem >>> hasName "tileset"
>>> getAttrR "firstgid" &&& ifA (hasAttr "source") (externalTileset mapPath) id
>>> tileset
externalTileset ∷ FilePath → IOSArrow XmlTree XmlTree
externalTileset mapPath =
arr (const (dropFileName mapPath)) &&& getAttrValue "source"
>>> arr (uncurry (</>))
>>> readFromDocument [ withValidate no, withWarnings yes ]
>>> getChildren >>> isElem >>> hasName "tileset"
tileset ∷ IOSArrow (Word32, XmlTree) Tileset
tileset = proc (tsInitialGid, ts) → do
tsName ← getAttrValue "name" ⤙ ts
tsTileWidth ← getAttrR "tilewidth" ⤙ ts
tsTileHeight ← getAttrR "tileheight" ⤙ ts
tsMargin ← arr (fromMaybe 0) . getAttrMaybe "margin" ⤙ ts
tsSpacing ← arr (fromMaybe 0) . getAttrMaybe "spacing" ⤙ ts
tsImages ← images ⤙ ts
tsTileProperties ← listA tileProperties ⤙ ts
returnA ⤙ Tileset {..}
where tileProperties ∷ IOSArrow XmlTree (Word32, Properties)
tileProperties = getChildren >>> isElem >>> hasName "tile"
>>> getAttrR "id" &&& properties
images = listA (getChildren >>> image)