简体   繁体   English

Java XML Transformer Node to String 删除命名空间

[英]Java XML Transformer Node to String remove namespace

I use the following method to transform an xml node to String (commonly found on the web):我使用以下方法将 xml 节点转换为 String(常见于网络上):

String nodeToString(Node node) {
   StringWriter sw = new StringWriter();
   try {
      Transformer t = TransformerFactory.newInstance().newTransformer();
      t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
      t.setOutputProperty(OutputKeys.INDENT, "yes");
      t.transform(new DOMSource(node), new StreamResult(sw));
   } catch (TransformerException te) {
     throw ...
   }
   return sw.toString();
}

The Node s transformed to String are not whole xml documents but just parts of a bigger XML.转换为 String 的Node不是整个 xml 文档,而只是更大 XML 的一部分。 The problem is that after transformed to String the root element of the Node has an xmlns added to it, which causes problems.问题是在转换为 String 后, Node的根元素添加了一个xmlns ,这会导致问题。

This is the String returned by nodeToString :这是nodeToString返回的字符串:

<doc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
     <header>
         ...
     </header>
     <payload>
         ...
     </payload>
</doc> 

Worst part is that this does not happen on my machine and the Test environment but only on the UAT and I struggle to find the difference between the environments.最糟糕的是,这不会发生在我的机器和测试环境上,而只会发生在 UAT 上,我很难找到环境之间的差异。

Does anybody bumped into this problem and know what causes it?有没有人遇到过这个问题并知道是什么原因造成的?

Edit:编辑:

The original document looks like this:原始文档如下所示:

<docs>
    <doc> 
         <header>
             ...
         </header>
         <payload>
             ...
         </payload>
    </doc>  
    <doc> 
         <header>
             ...
         </header>
         <payload>
             ...
         </payload>
    </doc>   
    // more <doc>s
<docs>  

It's splitted with XPath to Nodes (each Node is one element) then some business logic is applied (some nodes are removed, some are grouped differently) and at the end I have to turn it back to String.它使用 XPath 拆分为节点(每个节点是一个元素),然后应用一些业务逻辑(一些节点被删除,一些以不同的方式分组),最后我必须将其转回字符串。

Assuming that the xsi namespace is actually declared on some ancestor element, this is the correct behaviour, even though it might be problematic for you.假设 xsi 命名空间实际上是在某个祖先元素上声明的,这是正确的行为,即使它可能对您有问题。 In the XDM data model used by XSLT (and also by the JAXP identity transformer), an element has an in-scope namespace declaration for every namespace declared on that element or any of its ancestors, and when the element is serialized, all non-redundant in-scope namespace declarations are output.在 XSLT(以及 JAXP 身份转换器)使用的 XDM 数据模型中,对于在该元素或其任何祖先上声明的每个名称空间,元素都有一个范围内名称空间声明,并且当元素被序列化时,所有非输出冗余的范围内命名空间声明。

The reason for this is that some element or attribute might actually use the declared namespace prefix (this is common in XSLT and XSD but rare in other XML vocabularies).这样做的原因是某些元素或属性实际上可能使用声明的名称空间前缀(这在 XSLT 和 XSD 中很常见,但在其他 XML 词汇表中很少见)。

You can get rid of the unwanted namespaces by doing a transformation before serializing (with XSLT 2.0+ that's as simple as doing <xsl:copy-of copy-namespaces="no"/> ).您可以通过在序列化之前进行转换来摆脱不需要的名称空间(使用 XSLT 2.0+ 就像执行<xsl:copy-of copy-namespaces="no"/> )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM