好的,所以我以前从未使用过XSLT,我在网上看到了几个例子。但是,我并不完全确定每个示例在节点位置,模板匹配等方面的用途。
但实质上,我有这个XML文件,来自Activiti工作流程的示例流程定义:
<?xml version="1.0" encoding="UTF-8" ?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn"
xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC"
xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema"
expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://alfresco.org">
<process id="activitiAdhoc" name="Adhoc Activiti Process">
<startEvent id="start"
activiti:formKey="wf:submitAdhocTask" />
<sequenceFlow id='flow1'
sourceRef='start'
targetRef='adhocTask' />
<userTask id="adhocTask" name="Adhoc Task"
activiti:formKey="wf:adhocTask">
<extensionElements>
<activiti:taskListener event="create" class="org.alfresco.repo.workflow.activiti.tasklistener.ScriptTaskListener">
<activiti:field name="script">
<activiti:string>
if (typeof bpm_workflowDueDate != 'undefined') task.dueDate = bpm_workflowDueDate
if (typeof bpm_workflowPriority != 'undefined') task.priority = bpm_workflowPriority;
</activiti:string>
</activiti:field>
</activiti:taskListener>
</extensionElements>
<humanPerformer>
<resourceAssignmentExpression>
<formalExpression>${bpm_assignee.properties.userName}</formalExpression>
</resourceAssignmentExpression>
</humanPerformer>
</userTask>
<sequenceFlow id='flow2'
sourceRef='adhocTask'
targetRef='verifyTaskDone' />
<userTask id="verifyTaskDone" name="Verify Adhoc Task Completed."
activiti:formKey="wf:completedAdhocTask" >
<documentation>
Verify the arbitrary task was completed.
</documentation>
<extensionElements>
<activiti:taskListener event="create" class="org.alfresco.repo.workflow.activiti.tasklistener.ScriptTaskListener">
<activiti:field name="script">
<activiti:string>
if (typeof bpm_workflowDueDate != 'undefined') task.dueDate = bpm_workflowDueDate
if (typeof bpm_workflowPriority != 'undefined') task.priority = bpm_workflowPriority;
if (wf_notifyMe)
{
var mail = actions.create("mail");
mail.parameters.to = initiator.properties.email;
mail.parameters.subject = "Adhoc Task " + bpm_workflowDescription;
mail.parameters.from = bpm_assignee.properties.email;
mail.parameters.text = "It's done";
mail.execute(bpm_package);
}
</activiti:string>
</activiti:field>
</activiti:taskListener>
</extensionElements>
<humanPerformer>
<resourceAssignmentExpression>
<formalExpression>${initiator.exists() ? initiator.properties.userName : 'admin'}</formalExpression>
</resourceAssignmentExpression>
</humanPerformer>
</userTask>
<sequenceFlow id='flow3' sourceRef='verifyTaskDone'
targetRef='theEnd' />
<endEvent id="theEnd" />
</process>
<!-- Graphical representaion of diagram -->
<bpmndi:BPMNDiagram id="BPMNDiagram_activitiAdhoc">
<bpmndi:BPMNPlane bpmnElement="activitiAdhoc" id="BPMNPlane_activitiAdhoc">
<bpmndi:BPMNShape bpmnElement="start"
id="BPMNShape_start">
<omgdc:Bounds height="35" width="35" x="30" y="200"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="adhocTask"
id="BPMNShape_adhocTask">
<omgdc:Bounds height="55" width="105" x="130"
y="190"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="verifyTaskDone"
id="BPMNShape_verifyTaskDone">
<omgdc:Bounds height="55" width="105" x="290"
y="190"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="theEnd"
id="BPMNShape_theEnd">
<omgdc:Bounds height="35" width="35" x="455" y="200"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="65" y="217"></omgdi:waypoint>
<omgdi:waypoint x="130" y="217"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="235" y="217"></omgdi:waypoint>
<omgdi:waypoint x="290" y="217"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="395" y="217"></omgdi:waypoint>
<omgdi:waypoint x="455" y="217"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
我看到了here的一个例子:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/html/body/content/h2">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
<p><a href="...">your new link</a></p>
</xsl:template>
</xsl:stylesheet>
就我目前的目的而言,我并不关心将链接放在表单上的哪个位置,我只需要先显示它。所以我只是想知道我应该从该示例中更改哪些内容以匹配XML文件中的元素?
答案 0 :(得分:1)
我不确定我是否正确...这里有一些示例,您可以如何匹配文档的元素。但是有一些选择XSLT可能真的很复杂:-D 您必须始终牢记如何遍历节点树。
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:model="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:activiti="http://activiti.org/bpmn">
<!-- example match of definitions -->
<xsl:template match="model:definitions">
<!-- here you can do something -->
<!-- and if you want traverse the rest of the tree -->
<xsl:apply-templates select="*|@*"/>
</xsl:template>
<!-- example match of userTask -->
<xsl:template match="model:userTask">
<!-- here you can do something -->
<!-- and if you want traverse the rest of the tree -->
<xsl:apply-templates select="*|@*"/>
</xsl:template>
<!-- example match of an attribute -->
<xsl:template match="@activiti:formKey">
<!-- here you can do something -->
</xsl:template>
<!-- this handles all not explicit mentioned nodes ... -->
<xsl:template match="*|@*">
<!-- ... and traverse the xml tree -->
<xsl:apply-templates select="*|@*"/>
</xsl:template>
</xsl:stylesheet>