当前位置: 首页 > 知识库问答 >
问题:

OpenXML标记搜索

潘宸
2023-03-14

我正在编写一个。NET应用程序,它应该读取一个大约200页长的。docx文件(通常是documentformat.openxml2.5),以查找文档应该包含的某些标记的所有出现情况。明确地说,我不是在寻找OpenXML标记,而是应该由文档编写器设置到文档中的标记,作为在第二阶段需要填充的值的占位符。此类标记应采用以下格式:

 <!TAG!>

(其中TAG可以是任意字符序列)。正如我所说的,我必须找到这类标签的所有出现,加上(如果可能的话)定位找到标签出现的“页面”。我在web中发现了一些东西,但不止一次,基本方法是将文件的所有内容转储到字符串中,然后查看该字符串中的内容,而不考虑。docx编码。这要么导致假阳性,要么根本没有匹配(而test.docx文件包含几个标记),其他示例可能超出了我对OpenXML的了解。查找此类标记的regex模式应该是这样的:

<!(.)*?!>

我可以生成的最小的。docx示例将其存储在文档中

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 wp14">
<w:body>
<w:p w:rsidR="00CA7780" w:rsidRDefault="00815E5D">
  <w:pPr>
    <w:rPr>
      <w:lang w:val="en-GB"/>
    </w:rPr>
  </w:pPr>
  <w:r>
    <w:rPr>
      <w:lang w:val="en-GB"/>
    </w:rPr>
    <w:t>TRY</w:t>
  </w:r>
</w:p>
<w:p w:rsidR="00815E5D" w:rsidRDefault="00815E5D">
  <w:pPr>
    <w:rPr>
      <w:lang w:val="en-GB"/>
    </w:rPr>
  </w:pPr>
  <w:proofErr w:type="gramStart"/>
  <w:r>
    <w:rPr>
      <w:lang w:val="en-GB"/>
    </w:rPr>
    <w:t>&lt;!TAG1</w:t>
  </w:r>
  <w:proofErr w:type="gramEnd"/>
  <w:r>
    <w:rPr>
      <w:lang w:val="en-GB"/>
    </w:rPr>
    <w:t>!&gt;</w:t>
  </w:r>
</w:p>
<w:p w:rsidR="00815E5D" w:rsidRPr="00815E5D" w:rsidRDefault="00815E5D">
  <w:pPr>
    <w:rPr>
      <w:lang w:val="en-GB"/>
    </w:rPr>
  </w:pPr>
  <w:r>
    <w:rPr>
      <w:lang w:val="en-GB"/>
    </w:rPr>
    <w:t>TRY2</w:t>
  </w:r>
  <w:bookmarkStart w:id="0" w:name="_GoBack"/>
  <w:bookmarkEnd w:id="0"/>
</w:p>
<w:sectPr w:rsidR="00815E5D" w:rsidRPr="00815E5D">
  <w:pgSz w:w="11906" w:h="16838"/>
  <w:pgMar w:top="1417" w:right="1134" w:bottom="1134" w:left="1134" w:header="708" w:footer="708" w:gutter="0"/>
  <w:cols w:space="708"/>
  <w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
</w:document>

问候你,迈克

共有1个答案

东门楚
2023-03-14

试图查找标记的问题是,单词并不总是以它们在Word中显示的格式出现在底层XML中。例如,在示例XML中,<!tag1!>标记在多个运行中拆分,如下所示:

<w:r>
    <w:rPr>
        <w:lang w:val="en-GB"/>
    </w:rPr>
    <w:t>&lt;!TAG1</w:t>
</w:r>
<w:proofErr w:type="gramEnd"/>
    <w:r>
    <w:rPr>
        <w:lang w:val="en-GB"/>
    </w:rPr>
    <w:t>!&gt;</w:t>
</w:r>

正如注释中所指出的,这有时是由拼写和语法检查器造成的,但这并不是造成这种情况的全部原因。例如,在标记的某些部分上使用不同的样式也可能导致这种情况。

处理此问题的一种方法是查找段落innertext,并将其与regex进行比较。innertext属性将返回段落的纯文本,基础文档中没有任何格式或其他XML。

private static void ReplaceTags(string filename)
{
    Regex regex = new Regex("<!(.)*?!>", RegexOptions.Compiled);

    using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(filename, true))
    {
        //grab the header parts and replace tags there
        foreach (HeaderPart headerPart in wordDocument.MainDocumentPart.HeaderParts)
        {
            ReplaceParagraphParts(headerPart.Header, regex);
        }
        //now do the document
        ReplaceParagraphParts(wordDocument.MainDocumentPart.Document, regex);
        //now replace the footer parts
        foreach (FooterPart footerPart in wordDocument.MainDocumentPart.FooterParts)
        {
            ReplaceParagraphParts(footerPart.Footer, regex);
        }
    }
}

private static void ReplaceParagraphParts(OpenXmlElement element, Regex regex)
{
    foreach (var paragraph in element.Descendants<Paragraph>())
    {
        Match match = regex.Match(paragraph.InnerText);
        if (match.Success)
        {
            //create a new run and set its value to the correct text
            //this must be done before the child runs are removed otherwise
            //paragraph.InnerText will be empty
            Run newRun = new Run();
            newRun.AppendChild(new Text(paragraph.InnerText.Replace(match.Value, "some new value")));
            //remove any child runs
            paragraph.RemoveAllChildren<Run>();
            //add the newly created run
            paragraph.AppendChild(newRun);
        }
    }
}

上述方法的一个缺点是,您可能拥有的任何风格都将丢失。这些代码可以从现有的run复制,但是如果有多个run具有不同属性,则需要确定哪些代码需要复制到哪里。没有什么可以阻止您在上面的代码中创建多个run,如果需要的话,每个run都具有不同的属性。其他元素也会丢失(例如,任何符号),因此也需要说明这些元素。

 类似资料:
  • 我最近尝试使用JSoup来解析HTML文档,我在JSoup上做了一遍turrorious,发现select-Method可能就是我要找的。 我试图完成的是找到html文档中拥有某个类的所有元素。为了测试这一点,我在amazon的网页上尝试了这一点(想法:找到所有有特定优惠的交易)。 因此,我检查了web页面,看看正在使用哪些类和ID,然后尝试将其集成到一个小代码段中。在本例中,我找到了follwi

  • 我正在尝试使用OpenXML和埃里克·怀特的OpenXmlPowerTools(从NuGet安装)对. docx word文档进行基本的搜索和替换。我在这个网站和他的博客上遵循了一些例子,但是由于某种原因,当我运行代码后打开它时,我从来没有看到文档中出现的更改。这是我正在运行的简单函数: 消息框确实显示了它应该做的大量替换,但是当我打开文档时,我没有看到替换。此外,我认为我不需要那些文档。保存()

  • 我使用ElasticSearch对数据库进行索引。我试图使用edgeNGram标记器切割字符串,以射出要求“新字符串必须长于4个字符”的字符串。我使用以下代码创建索引: 现在我运行test来查看结果 并得到结果

  • mysql无法正确搜索标记。 当前我存储的标记如下所示- 当前我正在使用此代码目标id%1名称笔(记录%1) 但我得到这3张唱片是因为- 钢笔、铅笔、文字、编辑 货币、便士、美元 快速挂起 我只想要记录1,当我搜索关键字pen时

  • 最大ngram 36 在速度和内存方面,这会得到真正糟糕的加班吗?有没有更好的方法来部分搜索UUID?例如,我有7e222584-0818-49b0-875b-2774f4bf939b,我希望能够使用9b0搜索它

  • 我正在使用edge ngram标记器来提供部分匹配。我的文件看起来像 我的映射如下 以下查询给了我3个正确的文档(,,) 但是当我输入时,它会给我0个文档 我希望这将返回1个文档,但出于某种原因,它似乎没有索引令牌中的数字。让我知道,如果我错过了什么东西在这里。