【发布时间】:2016-09-30 07:50:48
【问题描述】:
我有以下 xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document mc:Ignorable="w14 w15 wp14" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mo="http://schemas.microsoft.com/office/mac/office/2008/main" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
<w:body>
<w:p w14:paraId="56037BEC" w14:textId="1188FA30" w:rsidR="001665B3" w:rsidRDefault="008B4AC6">
<w:r>
<w:t xml:space="preserve">This is the story of a man who </w:t>
</w:r>
<w:ins w:author="Mitchell Gould" w:date="2016-09-28T09:15:00Z" w:id="0">
<w:r w:rsidR="003566BF">
<w:t>went</w:t>
</w:r>
</w:ins>
<w:del w:author="Mitchell Gould" w:date="2016-09-28T09:15:00Z" w:id="1">
<w:r w:rsidDel="003566BF">
<w:delText>goes</w:delText>
</w:r>
</w:del>
...
我使用 Nokogiri 来解析 xml 如下:
zip = Zip::File.open("test.docx")
doc = zip.find_entry("word/document.xml")
file = Nokogiri::XML.parse(doc.get_input_stream)
我有一个包含所有 w:del 元素的“删除”节点集:
@deletions = file.xpath("//w:del")
我在这个节点集中搜索是否存在一个元素,如下所示:
my_node_set = @deletions.search("//w:del[@w:id='1']" && "//w:del/w:r[@w:rsidDel='003566BF']")
如果它存在,我想从删除节点集中删除它。我用以下方法做到这一点:
deletions.delete(my_node_set.first)
这似乎可以正常工作,因为没有返回任何错误,它会在终端中显示已删除的节点集。
但是,当我检查我的@deletions 节点集时,它似乎仍然存在:
@deletions.search("//w:del[@w:id='1']" && "//w:del/w:r[@w:rsidDel='003566BF']")
我只是想了解 Nokogiri,所以我显然没有在我的 @deletions 节点集中正确搜索元素,而是搜索整个文档。
如何在 @deletions 节点集中搜索元素,然后将其从节点集中删除?
【问题讨论】:
-
请阅读“minimal reproducible example”。我们需要一个语法正确的 XML 样本,它是演示问题所必需的最低限度。我建议也删除命名空间,因为它们与问题并不密切。
-
不清楚为什么要从 NodeSet 中选择性地删除。 NodeSet 就像指向文档中节点的指针数组。从数组中删除一个节点,实际上您所做的就是从树中删除该特定分支,换句话说,您正在从文档中删除该标签。如果您正在收集一堆节点,然后只想删除一个,那么最初只搜索那个并删除它。不要浪费时间和内存来收集 NodeSet。