我正在使用 XSLT 和 Saxon 转换一些 XML,如下所示:
Source sourceXML = new StreamSource(...);
Source sourceXSLT = new StreamSource(...);
System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
TransformerFactory transFact = TransformerFactory.newInstance();
Templates cachedXSLT = transFact.newTemplates(sourceXSLT);
Transformer transformer = cachedXSLT.newTransformer();
transformer.setOutputProperty("indent", "no");
transformer.transform(sourceXML, new StreamResult(System.out));
样式表是使用MapForce生成的,并且经常更改。虽然转换通常有效,但它会为所有元素添加名称空间n的前缀。这可能是由于样式表中的以下行:
<xsl:namespace-alias stylesheet-prefix="n" result-prefix="#default"/>
由于 MapForce 工具在其预览中没有显示此前缀,因此在转换器中更改此前缀可能非常容易。有人可以指出我正确的方向吗?还是我需要对样式表进行一些(手动)预处理才能摆脱它?
样式表的精简版本如下所示:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:n="..." xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:grp="http://www.altova.com/Mapforce/grouping" exclude-result-prefixes="fn grp xs xsi xsl" xmlns="...">
<xsl:namespace-alias stylesheet-prefix="n" result-prefix="#default"/>
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
...
</xsl:template>
</xsl:stylesheet>