0

我正在使用 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=...只保留在顶部节点。

任何替换attachNsEnv (toNsEnv [("w", wordNS)])uniqueNamespacesuniqueNamespacesFromDeclAndQNames导致根本没有命名空间声明的尝试。

我怎样才能真正清理我的 XML 输出?

4

1 回答 1

1

xmlns:w我确实实现了所需的输出,在顶级节点手动创建属性:

we::ArrowXml a=>String->a XmlTree XmlTree->a XmlTree XmlTree
we name child = w name [] [child]  

result::ArrowXml a=>a XmlTree XmlTree
result = 
  w "wordDocument" [sattr "xmlns:w" wordNS] [body children]
  where
    children = txt "Hello World!!!"
    body = we "body" . we "p" .  we "r"

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

于 2016-01-18T12:32:36.363 回答