Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xml-hamlet does not honour namespaces. #137

Open
fisx opened this issue Sep 11, 2018 · 2 comments
Open

xml-hamlet does not honour namespaces. #137

fisx opened this issue Sep 11, 2018 · 2 comments

Comments

@fisx
Copy link

fisx commented Sep 11, 2018

I want to render a piece of xml with namespaces, but the xmlns keyword is interpreted as an ordinary attribute:

{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}

import Text.Hamlet.XML as Hamlet
import Text.XML
import Text.XML.Cursor

test :: IO ()
test = parseSomething renderSomething

parseSomething :: Element -> IO ()
parseSomething el = do
  let cur = fromNode $ NodeElement el
      tick = cur $| element "{some:namespace}tick"
      tack = cur $/ element "{some:namespace}tack"

  print (length tick, length tack, cur)

renderSomething :: Element
renderSomething = el
  where
    [NodeElement el] = [Hamlet.xml|
      <tick xmlns="some:namespace">
          <tack>
      |]

expected behavior: output should be (1, 1, ..), and the xmlns keyword should not be in the attribute list.

observed behavior: output is (0, 0, ..), and the xmlns keyword is listed as an ordinary attribute.

@snoyberg
Copy link
Owner

I have no intention of modifying the xml-hamlet parser to have special support for xmlns right now. If you're interested in having this behavior, you'd need to send a PR yourself.

@fisx
Copy link
Author

fisx commented Sep 14, 2018

I'd love to fix this, but I don't think I'll find the time for this before xml comes out of fashion.

Oh, I just thought of something: what will go wrong if I replace Text.Hamlet.XMLParse.parseDoc with the xml-conduit parser?

workaround

-- | https://github.com/snoyberg/xml/issues/137
repairNamespaces :: HasCallStack => [Node] -> [Node]
repairNamespaces = fmap $ \case
  NodeElement el -> NodeElement $ repairNamespacesEl el
  other -> other

-- | https://github.com/snoyberg/xml/issues/137
repairNamespacesEl :: HasCallStack => Element -> Element
repairNamespacesEl el = unwrap . parseText def . renderText def . mkDocument $ el
  where
    unwrap (Right (Document _ el' _)) = el'
    unwrap (Left msg) = error $ show msg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants