首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >docx4j:帮助用MathML转换XHTML

docx4j:帮助用MathML转换XHTML
EN

Stack Overflow用户
提问于 2022-08-18 00:12:51
回答 1查看 62关注 0票数 0

我正在使用docx4j-ImportXHTML来转换XHTML文件。现在我想增加数学(方程式)的支持。我知道我需要包含一个XSL样式表来将MathML转换为OMML,但是我不知道如何添加XSL处理。有人能提供任何指导吗?

如有任何帮助,我们将不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2022-09-01 23:28:25

类似于:

代码语言:javascript
复制
 } else if  (e.getNodeName().equals("math")) {
                   
                    // handle me
                    System.out.println("Handling mathml \n\r" + XmlUtils.w3CDomNodeToString(e) );
                   
                            try {
                            // Prepare to transform Element e
                            Source xsltSource  = new StreamSource(
                                            ResourceUtils.getResource(
                                                            "mml2omml.xsl")
                                            ); // https://raw.githubusercontent.com/Marti ... l2omml.xsl
                            /* You need to add the template:
                             *
                                              <xsl:template match="/|*">
                                                <oMath>
                                                  <xsl:apply-templates mode="mml"  />
                                                </oMath>
                                              </xsl:template>

                             */
                            Templates xslt  = XmlUtils.getTransformerTemplate(xsltSource);                 
                           
                            // Use constructor which takes Unmarshaller, rather than JAXBContext,
                            // so we can set JaxbValidationEventHandler
                            JAXBContext jc = Context.jc;
                            Unmarshaller u = jc.createUnmarshaller();
                            u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler());
                            jakarta.xml.bind.util.JAXBResult result = new jakarta.xml.bind.util.JAXBResult(u );
                           
                            XmlUtils.transform(new DOMSource(e), xslt, null, result);
                           
                            // What happened?
                            org.docx4j.math.CTOMath math = (org.docx4j.math.CTOMath)XmlUtils.unwrap(result.getResult());
                           
                            org.docx4j.math.ObjectFactory mathObjectFactory = new org.docx4j.math.ObjectFactory();
                    // Create object for oMathPara (wrapped in JAXBElement)
                    CTOMathPara omathpara = mathObjectFactory.createCTOMathPara();
                    JAXBElement<org.docx4j.math.CTOMathPara> omathparaWrapped = mathObjectFactory.createOMathPara(omathpara);
                   
                    omathpara.getOMath().add(math);
                   
                    P wP = new P();
                    wP.getContent().add(omathparaWrapped);
                   
                            // Attach it to the document
                            this.contentContextStack.peek().getContent().add(wP);
                           
                            } catch (Exception e1) {
                                    throw new Docx4JException("Error processing MathML", e1);
                            }
                   
                    return;

有关更多细节,请参见https://www.docx4java.org/forums/xhtml-import-f28/issue-with-mathml-t3065.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73396150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档