excel - 从 DOCX4j 中的 xml 中删除不必要的命名空间

标签 excel xml namespaces docx4j

当我提取使用 docx4j 实现生成的 excel 时,我可以在下面看到额外的命名空间:

<xdr:wsDr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:cdr="http://schemas.openxmlformats.org/drawingml/2006/chartDrawing" xmlns:c14="http://schemas.microsoft.com/office/drawing/2007/8/2/chart" xmlns:dgm="http://schemas.openxmlformats.org/drawingml/2006/diagram" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:dsp="http://schemas.microsoft.com/office/drawing/2008/diagram" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:xvml="urn:schemas-microsoft-com:office:excel" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:pvml="urn:schemas-microsoft-com:office:powerpoint" xmlns:cppr="http://schemas.microsoft.com/office/2006/coverPageProps" xmlns:odx="http://opendope.org/xpaths" xmlns:odc="http://opendope.org/conditions" xmlns:odq="http://opendope.org/questions" xmlns:oda="http://opendope.org/answers" xmlns:odi="http://opendope.org/components" xmlns:odgm="http://opendope.org/SmartArt/DataHierarchy" xmlns:b="http://schemas.openxmlformats.org/officeDocument/2006/bibliography" xmlns:msink="http://schemas.microsoft.com/ink/2010/main" xmlns:cdr14="http://schemas.microsoft.com/office/drawing/2010/chartDrawing" xmlns:c15="http://schemas.microsoft.com/office/drawing/2012/chart" xmlns:cs="http://schemas.microsoft.com/office/drawing/2012/chartStyle" xmlns:ns38="http://www.w3.org/1998/Math/MathML" xmlns:ns39="http://www.w3.org/2003/InkML" xmlns:a13cmd="http://schemas.microsoft.com/office/drawing/2013/main/command" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:c16="http://schemas.microsoft.com/office/drawing/2014/chart" xmlns:dgm1611="http://schemas.microsoft.com/office/drawing/2016/11/diagram" xmlns:c173="http://schemas.microsoft.com/office/drawing/2017/03/chart" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:an18="http://schemas.microsoft.com/office/drawing/2018/animation" xmlns:anam3d="http://schemas.microsoft.com/office/drawing/2018/animation/model3d" xmlns:iact="http://schemas.microsoft.com/office/powerpoint/2014/inkAction" xmlns:thm15="http://schemas.microsoft.com/office/thememl/2012/main" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:wetp="http://schemas.microsoft.com/office/webextensions/taskpanes/2010/11" xmlns:we="http://schemas.microsoft.com/office/webextensions/webextension/2010/11" xmlns:comp="http://schemas.openxmlformats.org/drawingml/2006/compatibility" xmlns:lc="http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas" xmlns:dgm14="http://schemas.microsoft.com/office/drawing/2010/diagram" xmlns:a15="http://schemas.microsoft.com/office/drawing/2012/main" xmlns:pic14="http://schemas.microsoft.com/office/drawing/2010/picture" xmlns:c16ac="http://schemas.microsoft.com/office/drawing/2014/chart/ac" xmlns:a16="http://schemas.microsoft.com/office/drawing/2014/main" xmlns:a1611="http://schemas.microsoft.com/office/drawing/2016/11/main" xmlns:dgm1612="http://schemas.microsoft.com/office/drawing/2016/12/diagram" xmlns:a16svg="http://schemas.microsoft.com/office/drawing/2016/SVG/main" xmlns:adec="http://schemas.microsoft.com/office/drawing/2017/decorative" xmlns:a18hc="http://schemas.microsoft.com/office/drawing/2018/hyperlinkcolor" xmlns:wp15="http://schemas.microsoft.com/office/word/2012/wordprocessingDrawing">

我如何删除它?

代码实现链接:
Excel size decreases on resaving using docx4j library implementation

最佳答案

尝试使用 MOXy,或设置属性 docx4j.jaxb.marshal.canonicalize=true

来自 https://github.com/plutext/docx4j/blob/master/docx4j-samples-resources/src/main/resources/docx4j.properties#L160

# Whether to canonicalize during marshalling (defaults to false).  
# This is mainly for aesthetic purposes: the Sun/Oracle JAXB reference implementation
# writes a lot of unnecessary namespaces (each one in the JAXB context?).  MOXy is better.
# Set this to true to get rid of them via an extra step: canonicalization 
# since docx4j 3.3.1
docx4j.jaxb.marshal.canonicalize=false

关于excel - 从 DOCX4j 中的 xml 中删除不必要的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61635492/

相关文章:

excel - 找到单元格 block 中最后使用的单元格

vba - 在 Excel 中的图表中将文本框定位到顶部/左侧 -4

JavaScript 命名空间建议

xml - 具有命名空间的 PowerShell XML 选择节点

javascript - xml2json 从数组返回 1 个对象

C++ 命名空间规范是多余的还是有用的?

python - Xlsxwriter - 使用 xlsxwriter 格式化 Pandas 数据框单元格时遇到问题

Samba 网络驱动器上的 Excel 文件无法打开

java - Gson 可以生成 XML 吗?

xml - scala 2.11 中的 xml 模块化将如何处理 xml 文字?