ERROR: 'Namespace for prefix 'xsi' has

2019-08-15 08:48发布

Why am I getting this error:

ERROR: 'Namespace for prefix 'xsi' has not been declared.'

Here is my Java code:

package com.emp.ma.jbl.nsnhlrspmlpl.nsnhlrspmlpl.internal.action;
import com.emp.ma.util.xml.XMLDocument;
import com.emp.ma.util.xml.XMLDocumentBuilder;

 public class yay {            
        public static void main(String[] args) {
            XMLDocument xmldoc = XMLDocumentBuilder.newDocument().addRoot("spml:modifyRequest");
            xmldoc.gotoRoot().addTag("modification").addText("");
            xmldoc.gotoChild("modification").addTag("valueObject").addText("");
            xmldoc.gotoChild("valueObject").addAttribute("xsi:type","halo");
            System.out.println(xmldoc);
        }            
    }

This code was functioning properly until I tried throwing transformer exception whilst converting XML file to HTML for experimenting only. I need to create an xml file with the format:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<spml:modifyRequest>   
  <modification>
    <valueObject xsi:type="halo">
    </valueObject>   
  </modification>
</spml:modifyRequest>

I removed the transformer part from the code already and yet I'm getting this error in eclipse:

 ERROR:  'Namespace for prefix 'xsi' has not been declared.'
    Exception in thread "main" com.emp.ma.util.xml.XMLDocumentException: java.lang.RuntimeException: Namespace for prefix 'xsi' has not been declared.
        at com.emp.ma.util.xml.XMLDocumentImpl.toResult(XMLDocumentImpl.java:1244)
        at com.emp.ma.util.xml.XMLDocumentImpl.toStream(XMLDocumentImpl.java:1314)
        at com.emp.ma.util.xml.XMLDocumentImpl.toString(XMLDocumentImpl.java:1336)
        at com.emp.ma.util.xml.XMLDocumentImpl.toString(XMLDocumentImpl.java:1325)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.emp.ma.util.xml.XMLDocumentBuilder$XMLDocumentHandler.invoke(XMLDocumentBuilder.java:55)
        at $Proxy1.toString(Unknown Source)
        at java.lang.String.valueOf(Unknown Source)
        at java.io.PrintStream.println(Unknown Source)
        at com.emp.ma.jbl.nsnhlrspmlpl.nsnhlrspmlpl.internal.action.yay.main(yay.java:13)
    Caused by: javax.xml.transform.TransformerException: java.lang.RuntimeException: Namespace for prefix 'xsi' has not been declared.
        at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
        at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
        at com.emp.ma.util.xml.XMLDocumentImpl.toResult(XMLDocumentImpl.java:1242)
        ... 12 more
    Caused by: java.lang.RuntimeException: Namespace for prefix 'xsi' has not been declared.
        at com.sun.org.apache.xml.internal.serializer.SerializerBase.getNamespaceURI(Unknown Source)
        at com.sun.org.apache.xml.internal.serializer.SerializerBase.addAttribute(Unknown Source)
        at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
        at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
        at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
        at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
        at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
        at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(Unknown Source)
    ... 15 more

I'm stuck because of this one exception and I don't know how to undo this. Please do help. Like i said, it was functioning properly before trying this experiment of mine, if possible how do I remove this transformer integration. I've tried changing workspace as well -- still not working.

1条回答
神经病院院长
2楼-- · 2019-08-15 09:17

For an XML document to be well-formed, all used namespace prefixes must be declared.

Simply declare the xsi namespace prefix on the root element of your XML,

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<spml:modifyRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">   
  <modification>
    <valueObject xsi:type="halo">
    </valueObject>   
  </modification>
</spml:modifyRequest>

and your error will go away.

Note that you'll similarly have to define the spml namespace prefix.

查看更多
登录 后发表回答