HXT将XML命名空间仅附加到根元素

时间:2016-01-18 10:11:36

标签: xml haskell hxt

我正在使用ghc-7.10和hxt-9.3.1.15。我有简单的Open XML Word生成器编程

import Text.XML.HXT.Core
import System.Environment

readParams::IO (String, String)
readParams = do 
  args <- getArgs
  let defaultSrc = "methods.xml"
      defaultDst = "output.docx" 
  return $ case args of    
    [src]      -> (src, defaultDst)
    [src, dst] -> (src, dst)
    _other     -> (defaultSrc, defaultDst)

result::ArrowXml a=>a XmlTree XmlTree
result = structure where
  wordNS = "http://schemas.microsoft.com/office/word/2003/wordml"
  w      = mkqelem . flip (mkQName "w") wordNS
  structure =
    w "wordDocument" [] [
      w "body" [] [
        w "p" [] [
          w "r" [] [
            w "t" [] [txt "Hello World"]
          ]]]] 
    >>> attachNsEnv (toNsEnv [("w", wordNS)])


main::IO ()
main = do
  (src, dst) <- readParams
  _ <- runX $
    readDocument [withValidate no] src
    >>>
    root [] [ deep ( isElem >>> hasName "types" >>> result) ]
    >>>
    writeDocument[withIndent yes] dst 
  return ()

它正在生成有效的XML

<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
  <w:body xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
    <w:p xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
      <w:r xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
        <w:t xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">Hello World</w:t>
      </w:r>
    </w:p>
  </w:body>
</w:wordDocument>

但是我想将xmlns:w=...仅保留在顶级节点上。

任何使用uniqueNamespacesuniqueNamespacesFromDeclAndQNames替换attachNsEnv (toNsEnv [("w", wordNS)])的尝试都会产生没有名称空间声明的结果。

如何实际清理XML输出?

1 个答案:

答案 0 :(得分:1)

我确实在顶级节点上手动创建了 <html> <title>CAN Invoicing</title> <head> <script type="text/javascript"> function doTest() { alert("hello"); } </script> </head> <body onload="javascript:doTest();"> <div style="top:10;height:200px;"> <div style="float:left;"><a href="/"><img src="/images/Amazon-CAN-Logo.png" alt="CAN INVOICING"></img></a></div> <div style="float:right;top:200px;"><img src="/images/logo_csmetrics.gif" alt="Powered By CSBI"><img></div> </div> <div style="top:200px;"> <h1>you are authorized login User ::null</h1> <h2>Session username swasri</h2> <a href="/test/"target="_blank">test</a> <a href="/upload/" target="_blank">uplaod</a> <a href="/uploadS3/" target="_blank">s3</a> <a href="/readS3/" target="_blank">downloadS3</a> </div> <div><p>footer</p></div> </body> </html> 属性所需的输出:

xmlns:w

但我仍在寻找更方便的解决方案。