【发布时间】:2014-12-04 15:43:17
【问题描述】:
这是一个 XPath / OOXML 问题,供各位大师参考。
我有一个 MSOffice docx,其中包含不同颜色的突出显示文本,我必须将其转换为 XML,然后使用 XPath 识别所有这些突出显示的位置,无论颜色如何,以便可以在 XML 规则中过滤它们。
这是一个示例在 MSWord 中的样子
我了解如何使用 //w:p/w:r/w:rPr/w:highlight[@w:val='yellow']" 选择子节点(如果存在)
但如果在同一 <w:r> 块中存在高亮节点,我不能选择包含文本的实际 W:t 节点。
示例:如果父节点中存在<w:highlight>,我需要选择<W:t> 中的文本,并对文档中的所有情况执行此操作。
所以在本例中,我需要选择文本“This one goes because it is highlighted yellow”,因为它有一个 w:highlight 节点,其中 w:val 为黄色。
<w:r w:rsidRPr="003815B4">
<w:rPr>
<w:highlight w:val="yellow"/>
</w:rPr>
<w:t>This one goes because it is highlighted yellow</w:t>
</w:r>
任何帮助或指点将不胜感激:-)
这是 docx 的 xml 示例(为了便于阅读,删除了 OOXML 标头)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<doc>
<w:body>
<w:p w:rsidR="00B93038" w:rsidRDefault="003815B4">
<w:r>
<w:t>This line stays because it is not highlighted in any colour</w:t>
</w:r>
</w:p>
<w:p w:rsidR="003815B4" w:rsidRDefault="003815B4">
<w:r w:rsidRPr="003815B4">
<w:rPr>
<w:highlight w:val="yellow"/>
</w:rPr>
<w:t>This one goes because it is highlighted yellow</w:t>
</w:r>
</w:p>
<w:tbl>
<w:tblPr>
<w:tblStyle w:val="TableGrid"/>
<w:tblW w:w="0" w:type="auto"/>
<w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/>
</w:tblPr>
<w:tblGrid>
<w:gridCol w:w="4621"/>
<w:gridCol w:w="4621"/>
</w:tblGrid>
<w:tr w:rsidR="003815B4" w:rsidTr="003815B4">
<w:tc>
<w:tcPr>
<w:tcW w:w="4621" w:type="dxa"/>
</w:tcPr>
<w:p w:rsidR="003815B4" w:rsidRDefault="003815B4">
<w:r>
<w:t>And so on</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="4621" w:type="dxa"/>
</w:tcPr>
<w:p w:rsidR="003815B4" w:rsidRDefault="003815B4">
<w:r w:rsidRPr="003815B4">
<w:rPr>
<w:highlight w:val="cyan"/>
</w:rPr>
<w:t>Blue highlight</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
<w:tr w:rsidR="003815B4" w:rsidTr="003815B4">
<w:tc>
<w:tcPr>
<w:tcW w:w="4621" w:type="dxa"/>
</w:tcPr>
<w:p w:rsidR="003815B4" w:rsidRDefault="003815B4">
<w:r>
<w:t>Red</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="4621" w:type="dxa"/>
</w:tcPr>
<w:p w:rsidR="003815B4" w:rsidRDefault="003815B4">
<w:r>
<w:t xml:space="preserve">Mixed </w:t>
</w:r>
<w:r w:rsidRPr="003815B4">
<w:rPr>
<w:highlight w:val="red"/>
</w:rPr>
<w:t>text</w:t>
</w:r>
<w:r>
<w:t xml:space="preserve"> with </w:t>
</w:r>
<w:r w:rsidRPr="003815B4">
<w:rPr>
<w:highlight w:val="green"/>
</w:rPr>
<w:t>some highlighted</w:t>
</w:r>
<w:r>
<w:t xml:space="preserve"> and some not</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
<w:tr w:rsidR="003815B4" w:rsidTr="003815B4">
<w:tc>
<w:tcPr>
<w:tcW w:w="4621" w:type="dxa"/>
</w:tcPr>
<w:p w:rsidR="003815B4" w:rsidRDefault="003815B4"/>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="4621" w:type="dxa"/>
</w:tcPr>
<w:p w:rsidR="003815B4" w:rsidRDefault="003815B4"/>
</w:tc>
</w:tr>
</w:tbl>
<w:p w:rsidR="003815B4" w:rsidRDefault="003815B4"/>
<w:p w:rsidR="003815B4" w:rsidRDefault="003815B4">
<w:r>
<w:t>Another highlight</w:t>
</w:r>
</w:p>
<w:p w:rsidR="003815B4" w:rsidRDefault="003815B4">
<w:r>
<w:t>Some text</w:t>
</w:r>
</w:p>
<w:p w:rsidR="003815B4" w:rsidRDefault="003815B4"/>
<w:p w:rsidR="003815B4" w:rsidRDefault="003815B4">
<w:r>
<w:t>End</w:t>
</w:r>
<w:bookmarkStart w:id="0" w:name="_GoBack"/>
<w:bookmarkEnd w:id="0"/>
</w:p>
<w:sectPr w:rsidR="003815B4">
<w:pgSz w:w="11906" w:h="16838"/>
<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/>
<w:cols w:space="708"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
</doc>
【问题讨论】:
-
你在什么环境下使用XPath?
-
你试过了吗 //w:p/w:r/w:t[../w:rPr/w:highlight/@w:val='yellow']
-
Mathias 嗨,它是 Java 和 Sax 解析器。
-
Jason - 我想你有答案 - 直到明天早上我才在我的工作站前,但只是看着它似乎是解决方案!。忘记关卡了../
-
我认为这可行,但我有命名空间问题需要解决