我正在使用 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)])
为uniqueNamespaces
或uniqueNamespacesFromDeclAndQNames
导致根本没有命名空间声明的尝试。
我怎样才能真正清理我的 XML 输出?