我正在使用docx4j-ImportXHTML来转换XHTML文件。现在我想增加数学(方程式)的支持。我知道我需要包含一个XSL样式表来将MathML转换为OMML,但是我不知道如何添加XSL处理。有人能提供任何指导吗?
如有任何帮助,我们将不胜感激!
发布于 2022-09-01 23:28:25
类似于:
} 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。
https://stackoverflow.com/questions/73396150
复制相似问题