XSLT:更改变压器中的命名空间,而不是样式表中的命名空间/删除命名空间前缀
我正在使用 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>
I am transforming some XML using XSLT and Saxon like this:
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));
The stylesheet is a generated using MapForce and changed frequently. While the transformation works in general it prefixes all elements with the namespace n. This is presumably due to the following line in the stylesheet:
<xsl:namespace-alias stylesheet-prefix="n" result-prefix="#default"/>
Since the MapForce tool does not show this prefix in it's preview it's probably very easy to change this in the transformer. Can somebody point me in the right direction? Or do I need to do some (manual) preprocessing on the stylesheet in order to get rid of it?
The stripped down version of the stylesheet looks like this:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论