因工作需要實現公司自研的工作流bpmn規範化。研發過程總髮現需要學習xml schema來自定義本公司自身產品獨有的屬性。參考activity和camunda兩款工作流產品。現做幾點總結:
- 1.xml約束分dtd和schema兩種,dtd簡單容易上手,但是工作中卻基本不用,而是使用複雜的schema(支持更豐富和更復雜的數據類型)。網上的博客關於schema的基本只是入門(淺嘗輒止)難以符合真正開發需要。不過在菜鳥教程和w3c中都有完整的教程和使用案例,需要學習或者使用schema建議直接看這些教程而不是隻看博客。鏈接一:https://www.runoob.com/schema/schema-tutorial.html鏈接二:https://www.w3school.com.cn/schema/index.asp
- 2.關於schema中的命名空間,搜了一大圈感覺都沒有一個靠譜或者易懂的講解。最後我自己幾次嘗試後,給出以下解釋,命名空間的屬性和屬性值都是可以隨便自定義的,只要不和原有的重複既可以,多數是取自己公司的網站名,僅僅是一個標識作用而已。參考下面的Camunda xml的文件頭部定義xmlns:camunda="http://camunda.org/schema/1.0/bpmn" ,這裏的camunda可以改成任何不重複的字段,一般取自己產品的名字,這裏的“http://camunda.org/schema/1.0/bpmn”也不需要是個網址,任何唯一標識都可以,你可以隨意改成自己公司的網址也行,類似於一個聲明,我是唯一的,跟你們bpmn:不一樣,我也可以<camuda:..></camuda..> 寫自己的屬性。當然這裏要和schema中的這個要一致。
<?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:dc="http://www.omg.org/spec/DD/20100524/DC"
xmlns:di="http://www.omg.org/spec/DD/20100524/DI"
xmlns:camunda="http://camunda.org/schema/1.0/bpmn"
id="Definitions_0i8bey3"
targetNamespace="http://bpmn.io/schema/bpmn"
exporter="Camunda Modeler" exporterVersion="3.5.0">
<bpmn:process id="Process_07f8iwl" name="你好" isExecutable="true" camunda:versionTag="1.1.1">
<bpmn:startEvent id="StartEvent_1" name="開始">
<bpmn:outgoing>SequenceFlow_14ixhwl</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:endEvent id="EndEvent_11e4ewc" name="結束">
<bpmn:incoming>SequenceFlow_14ixhwl</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="SequenceFlow_14ixhwl" sourceRef="StartEvent_1" targetRef="EndEvent_11e4ewc" />
<bpmn:manualTask id="Task_12xfq2y" />
<bpmn:serviceTask id="Task_1cic714" />
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_07f8iwl">
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
<dc:Bounds x="179" y="249" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="186" y="285" width="22" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="EndEvent_11e4ewc_di" bpmnElement="EndEvent_11e4ewc">
<dc:Bounds x="452" y="249" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="459" y="285" width="22" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="SequenceFlow_14ixhwl_di" bpmnElement="SequenceFlow_14ixhwl">
<di:waypoint x="215" y="267" />
<di:waypoint x="452" y="267" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="ManualTask_06n8x6n_di" bpmnElement="Task_12xfq2y">
<dc:Bounds x="240" y="80" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="ServiceTask_03he7pw_di" bpmnElement="Task_1cic714">
<dc:Bounds x="490" y="90" width="100" height="80" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
- 3.schema教程中的複雜類型和anyattribute章節要細看。尤其是anyattribute可以解決xml在原有的屬性中嵌入一些新屬性。
- 4.關於bpmn規範化,bpmn中提供了<bpmn:extensionElements>元素可供各廠家拓展使用。
- 5.關於xml解析常使用dom4j,用xpath解析也很方便,注意使用xpath的時候不僅需要引入dom4j還要引入jaxen不然運行會報錯。
dependencies>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jaxen/jaxen -->
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
如有xml、schema約束不懂得地方歡迎留言探討,需要參考bpmn2.0規範化demo項目的同學也可以聯繫我,後面開源。