【问题标题】:How to parse mathML in output of WordOpenXML?如何在 WordOpenXML 的输出中解析 mathML?
【发布时间】:2013-05-26 12:11:14
【问题描述】:

我只想读取用于生成方程式的 xml,它是通过使用Paragraph.Range.WordOpenXML 获得的。但是用于等式的部分与MathML 不同,因为我发现微软的EquationMathML 中。

我需要使用一些特殊的转换器来获得所需的 xml 还是有其他方法?

【问题讨论】:

    标签: c# ms-word openxml openxml-sdk equations


    【解决方案1】:

    您可以使用OMML2MML.XSL 文件(位于%ProgramFiles%\Microsoft Office\Office15 下) 将 Word 文档中包含的 Microsoft Office MathML(方程式)转换为 MathML

    下面的代码展示了如何将 word 文档中的方程转换为 MathML 使用以下步骤:

    1. 使用 OpenXML SDK(2.5 版)打开 word 文档。
    2. 创建一个 XslCompiledTransform 并加载 OMML2MML.XSL 文件。
    3. 调用Transform()方法转换word文档 在创建的 XslCompiledTransform 实例上。
    4. 输出转换结果(例如在控制台打印或写入文件)。

    我已经用一个包含两个方程、文本和图片的简单 word 文档测试了下面的代码。

    using System.IO;
    using System.Xml;
    using System.Xml.Xsl;
    using DocumentFormat.OpenXml.Packaging;
    
    public string GetWordDocumentAsMathML(string docFilePath, string officeVersion = "14")
    {
        string officeML = string.Empty;
        using (WordprocessingDocument doc = WordprocessingDocument.Open(docFilePath, false))
        {
            string wordDocXml = doc.MainDocumentPart.Document.OuterXml;
    
            XslCompiledTransform xslTransform = new XslCompiledTransform();
    
            // The OMML2MML.xsl file is located under 
            // %ProgramFiles%\Microsoft Office\Office15\
            xslTransform.Load(@"c:\Program Files\Microsoft Office\Office" + officeVersion + @"\OMML2MML.XSL");
    
            using (TextReader tr = new StringReader(wordDocXml))
            {
                // Load the xml of your main document part.
                using (XmlReader reader = XmlReader.Create(tr))
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        XmlWriterSettings settings = xslTransform.OutputSettings.Clone();
    
                        // Configure xml writer to omit xml declaration.
                        settings.ConformanceLevel = ConformanceLevel.Fragment;
                        settings.OmitXmlDeclaration = true;
    
                        XmlWriter xw = XmlWriter.Create(ms, settings);
    
                        // Transform our OfficeMathML to MathML.
                        xslTransform.Transform(reader, xw);
                        ms.Seek(0, SeekOrigin.Begin);
    
                        using (StreamReader sr = new StreamReader(ms, Encoding.UTF8))
                        {
                            officeML = sr.ReadToEnd();
                            // Console.Out.WriteLine(officeML);
                        }
                    }
                }
            }
        }
        return officeML;
    }
    

    要仅转换一个方程(而不是整个 Word 文档),只需查询所需的 Office 数学段落 (m:oMathPara) 并使用此节点的 OuterXML 属性。 下面的代码展示了如何查询第一个数学段落:

    string mathParagraphXml = 
          doc.MainDocumentPart.Document.Descendants<DocumentFormat.OpenXml.Math.Paragraph>().First().OuterXml;
    

    使用返回的 XML 来提供 TextReader

    【讨论】:

    • 感谢@Hans,这也是一个很好的解决方案,我也想知道我是否也可以阅读段落文本和内联对象......??
    • 再次@Hans 它适用于整个文档的情况,但仅使用特定的 oMath 节点不起作用..?应该怎么做..?
    • @Hans 你能告诉我WordprocessingDocument 参考是什么吗?谢谢
    • @JeremyThompson:WordprocessingDocument 是一个代表 word 文档的类。该类包含在 Microsoft 的 OpenXML SDK 中。
    • 请注意,如果您在 Word 中工作,则不能替换 Document.Content.XML。您必须保存文档、关闭它或制作一个副本(当它在 Word 中打开时您无法阅读它),然后使用 OpenXML SDK 打开它。 Document.Content.XML 中的方程式被转义并嵌入到形状对象中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多