技术开发 频道

处理OpenOffice.org基于XML的文档格式


移除索引标记

    现在看看我之前提到的问题:如何移除多余的索引标记?清单 1 是一个示例 content.xml 文件,其中含有多处索引标记。

清单 1. 含有索引标记的 Content.xml

<text:p text:style-name="P3">A lot of people would think that you&apos;re out of your<text:alphabetical-index-mark text:string-value="gourd" text:key1="mind" text:key2="noggin"/> mind if you suggest such a thing. Some would say that the ubiquitous <text:alphabetical-index-mark-start text:id="IMark100896128"/>Microsoft <text:alphabetical-index-mark-end text:id="IMark100896128"/> <text:alphabetical-index-mark-start text:id="IMark101662028"/>Office <text:alphabetical-index-mark-end text:id="IMark101662028"/> is one of the greatest examples of &quot;feature creep&quot; of any program, with competitors like Corel and Open<text:alphabetical-index-mark-start text:id="IMark101661388"/> Office<text:alphabetical-index-mark-end text:id="IMark101661388"/>.org containing a comparable number of features.

    注意,此处有两种不同类型的索引标记。当 OpenOffice.org 把文档中的某个单词指定为标记时,会将 <text:alphabetical-index-mark-start><text:alphabetical-index-mark-end> 标记放在其两侧。指定给这两个标记的 text:id 属性是相同的,这样便可以识别它们是相互匹配的。

    如果索引中的单词没有出现在文本中,则会使用另外一种标记。如果多个关键字都与一个特定的位置相关,那么也会使用这种标记。在本例中,gourd 就是以这种方式输入的。我们可以看到它包含在 <text:alphabetical-index-mark> 标记中间,并没有使用 -start-end

    这种方式使得移除索引的方法极其简单。要解决此问题,只需在搜索替换操作中使用一些通配符。许多工具都可以完成该操作,但我认为使用 OpenOffice.org 的正则表达式引擎是惟一合适的方法:

  1. 在 OpenOffice.org 中打开 content.xml。
  2. 打开 Find & Replace 对话框并打开 More Options 选项。
  3. 选中 regular expressions 框。
  4. 在搜索框中输入 <text:alphabetical-index-mark(-(start|end))? [^>]*/>Replace with 输入框不填。
  5. 单击 Replace all
  6. 以原文本格式保存并关闭文档。

    大功告成!ODT 文件中的这个文件完成替换后,我们会发现所有索引引用都从文档中移除了。这种方法允许我们使用 OpenOffice.org 的工具创建索引,而不会产生重复的标记或者包含无用的标记。

    值得花时间琢磨一下上面所使用的这个搜索替换功能。<text:alphabetical-index-mark(-(start|end))? [^>]*/> 是一个模式,它有效地表示了任何 text:alphabetical-index-marktext:alphabetical-index-mark-starttext:alphabetical-index-mark-end tag 和它们的所有内容。也可以更简单的写为 <text:alphabetical-index-mark [^>]*/>,但是这样有可能会把其他一些类似命名的标记包括进来,如果有的话。

    注意,这一方法并不是万不一失的。比如说,如果某个索引标记的文本中包含有大于号 (>),那么这个功能会认为标记已经结束,而不会移除整个标记。此类潜在的问题的存在使我们有理由作好备份。如果发生这种问题,可以逐个执行搜索替换功能,这仍然是移除标记的最快捷的方法。除此之外,还可以编写一个更复杂正则表达式来解决这一问题。

0
相关文章