代码之家  ›  专栏  ›  技术社区  ›  yawn

XSLT:在transformer中更改名称空间,而不是在样式表中/删除名称空间前缀

  •  2
  • yawn  · 技术社区  · 15 年前

    我正在使用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工具在预览中不显示此前缀,因此在transformer中更改此前缀可能非常容易。谁能给我指一下正确的方向吗?或者我需要对样式表进行一些(手动)预处理以消除它吗?

    样式表的精简版本如下所示:

    <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>
    
    0 回复  |  直到 15 年前