java - 如何接受 docx 中的修订/跟踪更改 (ins/del)?

标签 java xslt ms-word docx4j wordml

在 MS-Word 2010 中,"file"->“信息”下有一个选项,可以在共享文档之前检查文档是否存在问题。这使得处理跟踪更改(到新的最新版本)并立即从文档中删除所有评论和注释成为可能。

enter image description here

这种可能性在 docx4j 中也可用吗?还是我需要研究相应的 JAXB 对象并编写一个遍历查找器? 手动执行此操作可能需要大量工作,因为我必须将 RunIns (w:ins) 添加到 R ( w:r)并删除 RunDel (w:del)。我还在 w:ins 中看到过一个 w:del。在这种情况下,我不知道这是否也会出现反之亦然或在更深的嵌套中。

进一步的研究提出了这个 XSLT: https://github.com/plutext/docx4all/blob/master/docx4all/src/main/java/org/docx4all/util/ApplyRemoteChanges.xslt 我无法在 docx4j 中运行它,而是通过手动解压缩 docx 并提取 document.xml 来运行。在纯 document.xml 上应用 xslt 后,我​​再次将其包装在 docx 容器中,以使用 MS-Word 打开它。结果与接受 MS-Word 本身的修订结果不同。更具体:XSLT 删除了已删除的标记文本(在表中),但没有删除文本之前的列表点。这在我的文档中经常出现。

如果这个请求无法以简单的方式解决,我将更改约束。对我来说,有一种方法来获取 ContentAccessor 的所有文本(作为 String)就足够了。 ContentAccessor 可以是 PTc。字符串应位于 R 内或 RunIns 内(其中包含 R) 为此,我在下面有一个半解决方案。有趣的部分从 else if (child instanceof RunIns) { 行开始。但如上所述,我不确定嵌套的 del/ins 语句会如何出现,以及这是否能很好地处理它们。结果还是和我之前用MS-Word准备文档不一样。

//Similar to:
//http://www.docx4java.org/forums/docx-java-f6/how-to-get-all-text-element-of-a-paragraph-with-docx4j-t2028.html
private String getAllTextfromParagraph(ContentAccessor ca) {
    String result = "";
    List<Object> children = ca.getContent();
    for (Object child : children) {
        child = XmlUtils.unwrap(child);
        if (child instanceof Text) {
            Text text = (Text) child;
            result += text.getValue();
        } else if (child instanceof R) {
            R run = (R) child;
            result += getTextFromRun(run);
        }
        else if (child instanceof RunIns) {
            RunIns ins = (RunIns) child;
            for (Object obj : ins.getCustomXmlOrSmartTagOrSdt()) {
                if (obj instanceof R) {
                    result += getTextFromRun((R) obj);
                }
            }
        }
    }
    return result.trim();
}

private String getTextFromRun(R run) {
    String result = "";
    for (Object o : run.getContent()) {
        o = XmlUtils.unwrap(o);
        if (o instanceof R.Tab) {
            Text text = new Text();
            text.setValue("\t");
            result += text.getValue();
        }
        if (o instanceof R.SoftHyphen) {
            Text text = new Text();
            text.setValue("\u00AD");
            result += text.getValue();
        }
        if (o instanceof Br) {
            Text text = new Text();
            text.setValue(" ");
            result += text.getValue();
        }
        if (o instanceof Text) {
            result += ((Text) o).getValue();
        }
    }
    return result;
}

最佳答案

https://github.com/plutext/docx4j/commit/309a8e4008553452ebe675e81def30aab97542a2?w=1添加了一种仅转换一个部件的方法,以及使用它接受更改的示例代码。

XSLT 正是您所发现的(重新授权为 Apache 2):

    <?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
  xmlns:o="urn:schemas-microsoft-com:office:office"
  xmlns:v="urn:schemas-microsoft-com:vml"
  xmlns:WX="http://schemas.microsoft.com/office/word/2003/auxHint"
  xmlns:aml="http://schemas.microsoft.com/aml/2001/core"
  xmlns:w10="urn:schemas-microsoft-com:office:word"
  xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:ext="http://www.xmllab.net/wordml2html/ext"
  xmlns:java="http://xml.apache.org/xalan/java"
  xmlns:xml="http://www.w3.org/XML/1998/namespace"
  version="1.0"
        exclude-result-prefixes="java msxsl ext o v WX aml w10">


  <xsl:output method="xml" encoding="utf-8" omit-xml-declaration="no" indent="yes" />


  <xsl:template match="/ | @*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="w:del" />

  <xsl:template match="w:ins" >
    <xsl:apply-templates select="*"/>
  </xsl:template>

</xsl:stylesheet>

您需要添加对 MSDN 链接中标识的其他元素的支持。如果您这样做,我很乐意收到拉取请求

关于java - 如何接受 docx 中的修订/跟踪更改 (ins/del)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45544974/

相关文章:

java - 哪个 Java 线程负责 PostgreSQL 数据库进程?

html - XSL : set image src using attribute 中的引号

c# - 使用 XmlCompiledTransform 合并两个 XPathDocuments

C# OpenXml 选择性字体加粗

java - JAVA中如何去除字符串中的转义字符

java - 将一个值转换为两个类

java - 如何比较java和mysql的日期

c# - 使用 C# 执行忽略 xsl :output 的 xslt 转换

ms-word - document.contentControls 未返回文档中的所有 ContentControls

vba - 错误替换双引号而不是双角引号 VBA Word