Saxon 添加随机 xmlns 属性

问题描述 投票:0回答:1

我用蜡染生成 svg。

然后我通过加载 svg 进行一些后期处理,修改它,然后将其保存回文件。

    public void doStuff(File sourceF, File destination) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        DocumentBuilder db;
        try {
            dbf.setValidating(false);
            dbf.setNamespaceAware(true);
            dbf.setFeature("http://xml.org/sax/features/namespaces", false);
            dbf.setFeature("http://xml.org/sax/features/validation", false);
            dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
            dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

            
            
            db = dbf.newDocumentBuilder();
            Document doc = db.parse(sourceF);
            
            //here I do some dom manipulation moving child around

            //then save :

            DOMSource source = new DOMSource(doc);
            FileOutputStream fos = new FileOutputStream(destination);
            Writer out = new OutputStreamWriter(fos, "UTF-8");
            StreamResult result = new StreamResult(out);

            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            transformer.transform(source, result);

默认内部jdk xerces工厂使用的是svg,它是正确的。

当我在类路径中有 Saxon 时,它使用 Saxon 中的 TransformerFactory。 输出几乎相同,但它在第一个“g”和“defs”上添加了 xmlns="",使其在浏览器中显示白色。

<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentScriptType="text/ecmascript" contentStyleType="text/css" height="847" preserveAspectRatio="xMidYMid meet" style="stroke-dasharray:none; shape-rendering:auto; font-family:'Dialog'; text-rendering:auto; fill-opacity:1; color-interpolation:auto; color-rendering:auto; font-size:12px; fill:black; stroke:black; image-rendering:auto; stroke-miterlimit:10; stroke-linecap:square; stroke-linejoin:miter; font-style:normal; stroke-width:1; stroke-dashoffset:0; font-weight:normal; stroke-opacity:1;" version="1.0" width="651" zoomAndPan="magnify">
    <!--Generated by the Batik Graphics2D SVG Generator-->
    <defs xmlns="" id="genericDefs">
        <style type="text/css" xml:space="preserve">svg { fill-rule: evenodd;pointer-events: none;}.hotspot { cursor: pointer;pointer-events: all;}@keyframes blink {100%,0% {fill: transparent;}60% {fill: #f00;}}.hotspotBlink {animation: blink 0.25s 3;}</style>
    </defs>
    <g xmlns="">

我可以强制 newInstance() 获得 xerces 实现,但如果我真的想使用 Saxon,我怎样才能避免添加这个 xmlns ?

java saxon
1个回答
0
投票

如果不确切知道您构建的 DOM 中的内容,就很难回答这个问题。 Saxon 显然认为

defs
g
元素不在命名空间中,并生成了
xmlns=""
属性来反映这一点。到底为什么得出这个结论还不清楚,因为你还没有显示构建 DOM 的代码。

请注意,您在这里使用 JAXP Transformer 来执行从 DOM 到序列化器的身份转换。但是序列化规范是根据 XDM 数据模型而不是 DOM 定义的,并且对于如何将任意 DOM 转换为 XDM 数据模型基本上没有明确的规范。这通常是边缘情况下的一个问题,在以编程方式构建的 DOM 中很容易出现这种问题,因为 DOM 施加的一致性约束非常少;例如,您可以在特定名称空间中创建元素,而无需为该名称空间创建元素声明,甚至可以在该名称空间的声明不一致的情况下; JAXP 的不同实现应该以不同的方式处理此类情况,这并不完全令人惊讶。

鉴于缺乏任何规范或商定的参考测试,在该领域应该不时发现并修复错误,这也不足为奇。因此,了解您正在使用哪个撒克逊版本会很有用。

© www.soinside.com 2019 - 2024. All rights reserved.