java - Namespace not declared at XML Node Transformation -
i trying process bpmn file own flow model. actually, problem not relate @ bpmn standard consider context issue. want xml node , transform string in order save later database. trying in following code bpmndiagram node, using xpath, , export string but, when try export expception not declaring nsi namespace. have declared namespaces @ xpath previous "query" once node , try transform it, error described below. xpath part working since getting right node. problem appears @ transformation phase.
xml file (partial)
<?xml version="1.0" encoding="utf-8"?> <bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/bpmn/20100524/model"xmlns:bpmndi="http://www.omg.org/spec/bpmn/20100524/di" xmlns:di="http://www.omg.org/spec/dd/20100524/di" xmlns:dc="http://www.omg.org/spec/dd/20100524/dc" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" id="definitions_1" targetnamespace="http://bpmn.io/schema/bpmn" exporter="camunda modeler" exporterversion="1.2.1"> <bpmn:process id="pp-processprova01" name="procesprova" isexecutable="true"> ... </bpmn:process> <bpmndi:bpmndiagram id="bpmndiagram_1"> <bpmndi:bpmnplane id="bpmnplane_1" bpmnelement="pp-processprova01"> <bpmndi:bpmnshape id="startevent_1cp968c_di" bpmnelement="pp_ev_entrada"> <dc:bounds x="-39" y="143" width="36" height="36" /> <bpmndi:bpmnlabel> <dc:bounds x="70" y="161" width="90" height="20" /> </bpmndi:bpmnlabel> </bpmndi:bpmnshape> <bpmndi:bpmnshape id="task_0ogrwwq_di" bpmnelement="pp_ac_validacion"> <dc:bounds x="241.17552742616033" y="120.96118143459915" width="100" height="80" /> </bpmndi:bpmnshape> <bpmndi:bpmnedge id="sequenceflow_1bc244v_di" bpmnelement="ev_tr_pp_ev_entrada-pp_ac_validacion"> <di:waypoint xsi:type="dc:point" x="-3" y="161" /> <di:waypoint xsi:type="dc:point" x="241" y="161" /> <bpmndi:bpmnlabel> <dc:bounds x="21.459854014598534" y="151" width="90" height="20" /> </bpmndi:bpmnlabel> </bpmndi:bpmnedge> </bpmndi:bpmnplane> </bpmndi:bpmndiagram> </bpmn:definitions>
this code:
string res=""; file file2 = new file("c:\\temp\\prova.bpmn"); documentbuilderfactory dbf = documentbuilderfactory.newinstance(); org.w3c.dom.document doc = dbf.newdocumentbuilder().parse(file2); hashmap<string, string> prefmap = new hashmap<string, string>() {{ put("bpmn", "http://www.omg.org/spec/bpmn/20100524/model"); put("bpmndi", "http://www.omg.org/spec/bpmn/20100524/di"); put("di", "http://www.omg.org/spec/dd/20100524/di"); put("dc", "http://www.omg.org/spec/dd/20100524/dc"); put("xsi", "http://www.w3.org/2001/xmlschema-instance"); put("camunda", "http://camunda.org/schema/1.0/bpmn"); }}; simplenamespacecontext namespaces = new simplenamespacecontext(prefmap); javax.xml.xpath.xpath xpath = xpathfactory.newinstance().newxpath(); xpath.setnamespacecontext(namespaces); javax.xml.xpath.xpathexpression expr = xpath.compile("/definitions/bpmndiagram"); node nodedi = (node) expr.evaluate(doc,xpathconstants.node); transformer t = transformerfactory.newinstance().newtransformer(); t.setoutputproperty(outputkeys.omit_xml_declaration, "yes"); t.transform(new domsource(nodedi), new streamresult(res));
error message:
namespace prefix 'nsi' has not been declared
do have declare in similar way namespaces @ transformation level? can anybody, please, me?
thanks in advance.
based on comment made martin honnen solve issue:
"note xslt , xpath need namespace aware documentbuilderfactory make sure first use setnamespaceaware(true) on documentbuilderfactory before creating documentbuilders , parsing xml documents namespaces"
Comments
Post a Comment