docx4j: 帮助使用 MathML 转换 XHTML

docx4j: help converting XHTML with MathML

提问人:Opusci 提问时间:8/18/2022 最后编辑:Opusci 更新时间:9/2/2022 访问量:131

问:

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

任何帮助将不胜感激!

java xhtml docx docx4j 数学

评论

0赞 JasonPlutext 8/23/2022
请在 docx4j 论坛上查看我对您原始帖子的回复,请不要交叉发帖。

答:

0赞 JasonPlutext 9/2/2022 #1

像这样:

 } 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