MS Word 以 XML 格式拆分单词

发布于 2024-08-11 02:25:27 字数 760 浏览 1 评论 0原文

我有一个以 WordProcessingML 格式保存为 XML 的 Word 2003 文档。它包含很少的占位符,这些占位符将被动态替换为适当的内容。 但是,问题是 Word 似乎随机地将它们分割成单独的单词。例如,而不是这样:

<w:t>${dl.d.out.ecs_rev}</w:t>

我有这样的:

...
<w:t>${</w:t>
 </w:r>
 <w:r wsp:rsidR="005D11C0">
  <w:rPr>
   <w:sz w:val="20" />
   <w:sz-cs w:val="20" />
  </w:rPr>
  <w:t>dl.</w:t>
 </w:r>
<w:r wsp:rsidRPr="00696324">
 <w:rPr>
  <w:sz w:val="20" />
  <w:sz-cs w:val="20" />
 </w:rPr>
<w:t>d.out.ecs_rev}</w:t>
...

有没有办法使用 Word 2003 保存“干净”的 XML 文档,或者是否有任何现有的解决方案可以进行清理?

我尝试用Java编写一种方法,将占位符的各个部分连接起来,但由于不同切割组合的数量相对较大,其算法比我必须做的原始任务复杂得多,所以它是自己的问题。

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content.
But, the problem is that Word seemingly randomly splits them in the separate words. For example, instead of this:

<w:t>${dl.d.out.ecs_rev}</w:t>

I have this:

...
<w:t>${</w:t>
 </w:r>
 <w:r wsp:rsidR="005D11C0">
  <w:rPr>
   <w:sz w:val="20" />
   <w:sz-cs w:val="20" />
  </w:rPr>
  <w:t>dl.</w:t>
 </w:r>
<w:r wsp:rsidRPr="00696324">
 <w:rPr>
  <w:sz w:val="20" />
  <w:sz-cs w:val="20" />
 </w:rPr>
<w:t>d.out.ecs_rev}</w:t>
...

Is there any way to save a "clean" XML document using Word 2003, or is there any existing solution which can do the cleaning?

I tried to program a method in Java which will concatenate separated parts of the placeholders, but because the number of different cutting combinations is relatively big, the algorithm for that is far more complex than a original task that I have to do, so it is problem for itself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

走走停停 2024-08-18 02:25:27

如果您可以控制原始 Word 文档,则可以阻止 Word 插入 rsid 并突出显示语法/拼写错误。

         Word.Options opts = Word.Options;
            opts.CheckGrammarAsYouType = false;
            opts.CheckGrammarWithSpelling = false;
            opts.CheckSpellingAsYouType = false;
            opts.StoreRSIDOnSave = false;

例如,如果您更改单词的部分字体,单词仍然会被分割。

嗯,我有一个简单+丑陋的 xslt,我用它来清理 WordML,就像您发布的示例一样。如果您需要的话,我可以将其提交给 docx4j,但正如您所说,有多种组合不会被涵盖。不管怎样,如果你想要它,请发帖到 docx4j 论坛。

更可靠的方法是提取纯文本,并将纯文本与 XML 相关联,这样您就可以搜索纯文本,并从那里转到 XML。

If you have control over the original Word documents, you can stop Word from inserting rsid and highlighting grammar/spelling errors.

         Word.Options opts = Word.Options;
            opts.CheckGrammarAsYouType = false;
            opts.CheckGrammarWithSpelling = false;
            opts.CheckSpellingAsYouType = false;
            opts.StoreRSIDOnSave = false;

Words will still get split, if for example you change font part way through the word.

Hmmm, I have a simple+ugly bit of xslt which I've used to clean WordML like the example you posted. I could commit it to docx4j if you want it, but as you say, there are various combinations which wouldn't be covered. Anyway, if you want it, please post to the docx4j forum.

A more robust approach would be to extract the plain text, and relate the plain text to the XML, so you can search the plain text, and go from there to the XML.

淡忘如思 2024-08-18 02:25:27

Word 2003 XML 异常复杂且难以解码。您获得多个标签的原因是 Word ML 生成称为“运行”的标签(w:r 标签)。据我所知,没有简单的方法来清理上面的 XML。我建议使用 HTML 而不是 WordML。使用适当的内容来操作和替换占位符要容易得多。
如果成本不是目标,请使用 Aspose 这样的产品。它为您做一切事情并且使用简单。

Word 2003 XML is unusually complex and hard to decode. The reason you are getting multiple tags is because Word ML generates tags called runs (the w:r tag). As far as I know, there is no easy way to do the clean the XML above. I would recommend using HTML instead of WordML. It is way easier to manipulate and replace your placeholders with appropriate content.
If cost is not an objective, use a product like Aspose. It does everything for you and is simple to use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文