Search code examples
xmlhaskellxml-conduit

xml-conduit parse xml attributes


Parsing XML with xml-conduit I stumbled upon the following problem: when I have multiple attributes, with the same base name but different prefixes only the first in (lexical) order.

How can I get the prefixed values if both a prefixed and non-prefixed version of an attribute is present?

Minimal non-working example:

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")])

Solution

  • Citing Text.XML.Name:

    Prefixes are not semantically important; they are included only to simplify pass-through parsing. When comparing names with Eq or Ord methods, prefixes are ignored.

    The semantic difference lies in the namespace, so the following solves your problem:

    test :: [Text]
    test =["<Root xmlns:is=\"http://example.com\" xmlns:not=\"http://example.com/2\""
          ,  "here    = \"ok\""
          ,  "is:here = \"ok\""
          ,  "not:here=\"nok\">"
          ,"</Root>"]
    

    This also makes sense, because we could name the same namespace differently in different places, but it should still be the same. I think it's also no valid XML to use prefixes without associating namespaces to them.