xml-conduit解析xml属性

时间:2015-12-14 12:06:37

标签: xml haskell xml-conduit

使用xml-conduit解析XML我偶然发现了以下问题:当我有多个属性时,使用相同的基本名称但不同的前缀只有第一个(词法)顺序。

如果属性的前缀和非前缀版本都存在,如何获取前缀值?

最小的非工作示例:

Main.hs

{-# LANGUAGE OverloadedStrings #-}

module Main where

import           Data.Text.Lazy (Text)
import qualified Data.Text.Lazy as T
import           Text.XML (parseText, def, elementAttributes, documentRoot)
import           Data.List (splitAt, drop)

main :: IO ()
main = do
  putStrLn "Example1: only the first element is parsed"
  putStrLn "========\n"
  print $ elementAttributes . documentRoot <$> parseText def (T.unlines test)
  putStrLn "Example2: this behaviour is independent of both having a prefix"
  putStrLn "========\n"
  print $ elementAttributes . documentRoot <$> parseText def (T.unlines $ dropAt 1 test)
  putStrLn "Example3: also no difference if there is just one attribute with prefix"
  putStrLn "========\n"
  print $ elementAttributes . documentRoot <$> parseText def (T.unlines $ dropAt 2 test)
  putStrLn "Example4: on its own the last element can be parsed"
  putStrLn "========\n"
  print $ elementAttributes . documentRoot <$> parseText def (T.unlines $ dropAt 1 $ dropAt 1 test)
  putStrLn "==============="
  putStrLn "Example1: it is always the first element parsed"
  putStrLn "========\n"
  print $ elementAttributes . documentRoot <$> parseText def (T.unlines test2)
  putStrLn "Example2: really just the first"
  putStrLn "========\n"
  print $ elementAttributes . documentRoot <$> parseText def (T.unlines $ dropAt 1 test2)


test :: [Text]
test =["<Root"
      ,  "here    = \"ok\""
      ,  "is:here = \"ok\""
      ,  "not:here=\"nok\">"
      ,"</Root>"]

test2 :: [Text]
test2 =["<Root"
       ,  "is:here = \"ok\""
       ,  "here    = \"ok\""
       ,  "not:here=\"nok\">"
       ,"</Root>"]

dropAt :: Int -> [a] -> [a]
dropAt i xs = let (hd,tl) = splitAt i xs
              in hd ++ drop 1 tl

attr.cabal

build-depends: base >= 4.7 && < 5
             , xml-conduit
             , text

> stack exec attr
Example1: only the first element is parsed
========

Right (fromList [(Name {nameLocalName = "here", nameNamespace = Nothing, namePrefix = Nothing},"ok")])
Example2: this behaviour is independent of both having a prefix
========

Right (fromList [(Name {nameLocalName = "here", nameNamespace = Nothing, namePrefix = Just "is"},"ok")])
Example3: also no difference if there is just one attribute with prefix
========

Right (fromList [(Name {nameLocalName = "here", nameNamespace = Nothing, namePrefix = Nothing},"ok")])
Example4: on its own the last element can be parsed
========

Right (fromList [(Name {nameLocalName = "here", nameNamespace = Nothing, namePrefix = Just "not"},"nok")])
===============
Example1: only the first element is parsed
========

Right (fromList [(Name {nameLocalName = "here", nameNamespace = Nothing, namePrefix = Just "is"},"ok")])
Example2: this behaviour is independent of both having a prefix
========

Right (fromList [(Name {nameLocalName = "here", nameNamespace = Nothing, namePrefix = Nothing},"ok")])

1 个答案:

答案 0 :(得分:1)

引用self.incidents()

  

前缀在语义上不重要;它们仅用于简化传递解析。将名称与Eq或Ord方法进行比较时,前缀将被忽略。

语义差异在于命名空间,因此以下内容可以解决您的问题:

Text.XML.Name

这也是有道理的,因为我们可以在不同的地方以不同的名称命名相同的命名空间,但它应该仍然是相同的。我认为使用前缀而不将名称空间与它们相关联也没有有效的XML。