从元素树转换为属性树

时间:2010-07-31 12:21:56

标签: xslt

我的源文件是:

<?xml version="1.0" encoding="UTF8"?>
 <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:fn="http://www.w3.org/2005/xpathfunctions" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xsi:schemaLocation="http://schema.omg.org/spec/BPMN/2.0 schema\BPMN20.xsd" targetNamespace="http://schema.omg.org/spec/BPMN/2.0">
 <process id="DA444C7EB7A92BC0EC615091EFE52E9F" name="Process111">
  <laneSet>
    <lane id="BE805267E22661E25210E901418B7DA2" name="Lane111" > 
     <flowNodeRefs>E705CE86B8897B266BA6AC81E2F1A5BC</flowNodeRefs>     
     <flowNodeRefs>643E3A26322BEECB2DBD84BF2A6F9CA5</flowNodeRefs>
    </lane>
    <lane id="2F92C8B27CE52AFDB382D4EAC634E634" name="Lane222" > 
     <flowNodeRefs>A65A0630689FA02F3BBB6AC27D906603</flowNodeRefs>     
     <flowNodeRefs>DDA49A7836C13B7DC3208778C4B03526</flowNodeRefs>
  </lane>
   </laneSet>
  <startEvent id="E705CE86B8897B266BA6AC81E2F1A5BC" name="Start111" /> 
  <endEvent id="643E3A26322BEECB2DBD84BF2A6F9CA5" name="End111" /> 
  <startEvent id="A65A0630689FA02F3BBB6AC27D906603" name="Start222" /> 
  <terminateEvent id="DDA49A7836C13B7DC3208778C4B03526" name="Terminate111" /> 
  </process>
 <process id="A7F84E7EA73F9436DCB925D5C3208227" name="Process2222">
 <laneSet>
  <lane id="B8EE1D7AA494C6D25656434F1CAF803F" name="Lane444" /> 
  </laneSet>
  </process>
  </definitions>

我想转换为目标文档:

<?xml version="1.0" encoding="UTF-8"?>  
<svg  version="1.1"  xmlns="http://www.w3.org/2000/svg"  xmlns.x="org.w3c.dom.svg"  xmlns:xlink="http://www.w3.org/1999/xlink"  >
 <g id="BPMNDIAGRAM" transform="scale(1)"> 
 <g id="POOLS" class="POOLS">
 <g id="DA444C7E-B7A9-2BC0-EC61-5091EFE52E9F" class="POOL" >
  <g >
   <text >
   <tspan >Process111</tspan>
   </text>
  </g>
  <g id="BE805267-E226-61E2-5210-E901418B7DA2" class="LANE"  PoolID="DA444C7E-B7A9-2BC0-EC61-5091EFE52E9F">
   <g>
    <text >
    <tspan>Lane111</tspan>
    </text>
   </g>
   <g id="EVENTS_BE805267-E226-61E2-5210-E901418B7DA2" class="EVENTS">
    <g id="643E3A26-322B-EECB-2DBD-84BF2A6F9CA5" class="ENDEVENT"  LaneID="BE805267-E226-61E2-5210-E901418B7DA2">
     <text >
     <tspan >End111</tspan>
     </text>
    </g>
    <g id="E705CE86-B889-7B26-6BA6-AC81E2F1A5BC" class="STARTEVENT"  LaneID="BE805267-E226-61E2-5210-E901418B7DA2">
      <text >
      <tspan x="0" dy="10">Start111</tspan>
      </text>
    </g>
   </g>
  </g>
  <g id="2F92C8B2-7CE5-2AFD-B382-D4EAC634E634" class="LANE"  PoolID="DA444C7E-B7A9-2BC0-EC61-5091EFE52E9F">
   <g >
    <text >
    <tspan x="0" dy="10">Lane222</tspan>
    </text>
   </g>
   <g id="EVENTS_2F92C8B2-7CE5-2AFD-B382-D4EAC634E634" class="EVENTS">
    <g id="A65A0630689FA02F3BBB6AC27D906603" class="STARTEVENT"  LaneID="2F92C8B2-7CE5-2AFD-B382-D4EAC634E634">
      <text >
      <tspan x="0" dy="10">Start222</tspan>
      </text>
    </g>
    <g id="DDA49A78-36C1-3B7D-C320-8778C4B03526" class="TERMINATEEVENT"  LaneID="2F92C8B2-7CE5-2AFD-B382-D4EAC634E634">
     <text >
     <tspan x="0" dy="10">Terminate111</tspan>
     </text>
    </g>    
   </g>   
  </g>
 </g>
 <g id="A7F84E7E-A73F-9436-DCB9-25D5C3208227" class="POOL" > 
  <g >
   <text >
    <tspan >Process2222</tspan>
   </text>
  </g>

 <g id="B8EE1D7A-A494-C6D2-5656-434F1CAF803F" class="LANE"  PoolID="A7F84E7E-A73F-9436-DCB9-25D5C3208227">
  <g >
   <text >
    <tspan>Lane444</tspan>
   </text>
  </g>     
 </g>
 </g>
 </g>
</g>
</svg>

我的转换文件如下代码:

<?xml version="1.0" encoding="Windows-1252"?>
<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:svg="http://www.w3.org/2000/svg" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xlink="http://www.w3.org/1999/xlink">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <!-- BPMN 2.0 Transformation: BPMN2.0 xml-to-SVG xml -->    
  <xsl:strip-space elements="*"/>
    <xsl:template match="/">
        <svg>
            <xsl:attribute name="xmlns.x">org.w3c.dom.svg</xsl:attribute>   
            <xsl:attribute name="targetNamespace">http://schema.omg.org/spec/BPMN/2.0</xsl:attribute>
            <xsl:call-template name="rootElementTemplate"/>                     
        </svg>      
    </xsl:template>
    <!-- rootElementTemplate -->
    <xsl:template name="rootElementTemplate">
        <g>             
         <!--call the other templates-->
            <xsl:call-template name="pools"/>
        </g>
    </xsl:template>
    <xsl:template name="pools">
        <g>
            <xsl:attribute name="id">POOLS</xsl:attribute>
            <xsl:attribute name="class">POOLS</xsl:attribute>
            <xsl:apply-templates select="process"/>
        </g>
    </xsl:template>

<xsl:template match="process">
    <g>
      <xsl:attribute name="id"><xsl:value-of select="."/></xsl:attribute> 
      <xsl:attribute name="class">POOL</xsl:attribute>
        <xsl:call-template name="text1"/>
    </g>
</xsl:template>

<xsl:template name="text1" >
<xsl:for-each select="@name">
 <g >
    <xsl:element name="text">
        <xsl:element name="tspan"><xsl:value-of select= "."/></xsl:element>
    </xsl:element>  
 </g>
 </xsl:for-each>
</xsl:template>

<xsl:template match="lane">
  <g id="{@id}" class="LANE" >
      <xsl:apply-templates select="@name"/>
     <xsl:variable name="p" select="count(preceding-sibling::lane)+1"/>
     <xsl:variable name="ref" select="ancestor::process/*[$p+1]"/>
     <xsl:apply-templates select="$ref"/>
  </g>
</xsl:template>

<xsl:template match="startevent">
  <g id="{@id}" class="STARTEVENT" name="{@name}" />
</xsl:template>

</xsl:stylesheet>

我不知道为什么它不起作用?请帮帮我

1 个答案:

答案 0 :(得分:0)

此样式表:

<xsl:stylesheet version="1.0"
exclude-result-prefixes="m"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:x="org.w3c.dom.svg">
    <xsl:variable name="attrSet">
        <dummy x="0" dy="10"/>
    </xsl:variable>
    <xsl:template match="m:definitions">
        <svg version="1.1">
            <g id="BPMNDIAGRAM" transform="scale(1)">
                <g id="POOLS" class="POOLS">
                    <xsl:apply-templates />
                </g>
            </g>
        </svg>
    </xsl:template>
    <xsl:template match="m:process">
        <g class="POOL">
            <xsl:apply-templates select="@id"/>
            <xsl:apply-templates select="@name|m:laneSet/*"/>
        </g>
    </xsl:template>
    <xsl:template match="*">
        <xsl:param name="idName" select="'PoolID'"/>
        <xsl:param name="id" select="../../@id"/>
        <g class="{translate(name(),
                             'lanedvtsrmi',
                             'LANEDVTSRMI')}">
            <xsl:attribute name="{$idName}">
                <xsl:apply-templates select="$id" mode="id"/>
            </xsl:attribute>
            <xsl:apply-templates select="@id"/>
            <xsl:apply-templates select="@name"/>
            <xsl:apply-templates/>
        </g>
    </xsl:template>
    <xsl:template match="m:flowNodeRefs[position()!=1]"/>
    <xsl:template match="m:flowNodeRefs[1]">
        <g class="EVENTS">
            <xsl:attribute name="id">
                <xsl:text>EVENTS_</xsl:text>
                <xsl:apply-templates select="../@id" mode="id"/>
            </xsl:attribute>
            <xsl:apply-templates select="../../../*[@id = current()/../*]">
                <xsl:with-param name="idName" select="'LaneID'"/>
                <xsl:with-param name="id" select="../@id"/>
            </xsl:apply-templates>
        </g>
    </xsl:template>
    <xsl:template match="@id">
        <xsl:attribute name="id">
            <xsl:apply-templates select="." mode="id"/>
        </xsl:attribute>
    </xsl:template>
    <xsl:template match="@id" mode="id">
        <xsl:value-of select="concat(substring(.,1,8),'-',
                                     substring(.,9,4),'-',
                                     substring(.,13,4),'-',
                                     substring(.,17,4),'-',
                                     substring(.,21))"/>
    </xsl:template>
    <xsl:template match="@name">
        <g>
            <text>
                <tspan>
                    <xsl:copy-of select="document('')/*/xsl:variable
                                    [@name='attrSet']/*/@*
                                    [not(current()/../self::m:endEvent|
                                         current()/../self::m:process)]
                                    [not(generate-id(current()/..) =
                                         generate-id(current()/../../m:lane[1]))]"/>
                    <xsl:value-of select="."/>
                </tspan>
            </text>
        </g>
    </xsl:template>
</xsl:stylesheet>

输出:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:x="org.w3c.dom.svg">
    <g id="BPMNDIAGRAM" transform="scale(1)">
        <g id="POOLS" class="POOLS">
            <g class="POOL" id="DA444C7E-B7A9-2BC0-EC61-5091EFE52E9F">
                <g>
                    <text>
                        <tspan>Process111</tspan>
                    </text>
                </g>
                <g class="LANE" PoolID="DA444C7E-B7A9-2BC0-EC61-5091EFE52E9F" id="BE805267-E226-61E2-5210-E901418B7DA2">
                    <g>
                        <text>
                            <tspan>Lane111</tspan>
                        </text>
                    </g>
                    <g class="EVENTS" id="EVENTS_BE805267-E226-61E2-5210-E901418B7DA2">
                        <g class="STARTEVENT" LaneID="BE805267-E226-61E2-5210-E901418B7DA2" id="E705CE86-B889-7B26-6BA6-AC81E2F1A5BC">
                            <g>
                                <text>
                                    <tspan x="0" dy="10">Start111</tspan>
                                </text>
                            </g>
                        </g>
                        <g class="ENDEVENT" LaneID="BE805267-E226-61E2-5210-E901418B7DA2" id="643E3A26-322B-EECB-2DBD-84BF2A6F9CA5">
                            <g>
                                <text>
                                    <tspan>End111</tspan>
                                </text>
                            </g>
                        </g>
                    </g>
                </g>
                <g class="LANE" PoolID="DA444C7E-B7A9-2BC0-EC61-5091EFE52E9F" id="2F92C8B2-7CE5-2AFD-B382-D4EAC634E634">
                    <g>
                        <text>
                            <tspan x="0" dy="10">Lane222</tspan>
                        </text>
                    </g>
                    <g class="EVENTS" id="EVENTS_2F92C8B2-7CE5-2AFD-B382-D4EAC634E634">
                        <g class="STARTEVENT" LaneID="2F92C8B2-7CE5-2AFD-B382-D4EAC634E634" id="A65A0630-689F-A02F-3BBB-6AC27D906603">
                            <g>
                                <text>
                                    <tspan x="0" dy="10">Start222</tspan>
                                </text>
                            </g>
                        </g>
                        <g class="TERMINATEEVENT" LaneID="2F92C8B2-7CE5-2AFD-B382-D4EAC634E634" id="DDA49A78-36C1-3B7D-C320-8778C4B03526">
                            <g>
                                <text>
                                    <tspan x="0" dy="10">Terminate111</tspan>
                                </text>
                            </g>
                        </g>
                    </g>
                </g>
            </g>
            <g class="POOL" id="A7F84E7E-A73F-9436-DCB9-25D5C3208227">
                <g>
                    <text>
                        <tspan>Process2222</tspan>
                    </text>
                </g>
                <g class="LANE" PoolID="A7F84E7E-A73F-9436-DCB9-25D5C3208227" id="B8EE1D7A-A494-C6D2-5656-434F1CAF803F">
                    <g>
                        <text>
                            <tspan>Lane444</tspan>
                        </text>
                    </g>
                </g>
            </g>
        </g>
    </g>
</svg>

注意:这些行

<xsl:apply-templates select="@id"/>
<xsl:apply-templates select="@name|m:laneSet/*"/>

并且

<xsl:apply-templates select="@id"/>
<xsl:apply-templates select="@name"/>
<xsl:apply-templates/>

可能像

<xsl:apply-templates select="@*|m:laneSet/*"/>

<xsl:apply-templates select="@*|*"/>

但是,从规范中无法保证属性是以任何顺序进行处理的(除了子元素之前)。

另外,我使用一种棘手的方法来添加xdy属性。但谓词中的条件可用作xsl:if的测试,并使用xsl:attribute添加这些属性。