开发者

how to transform with this source and target document?

开发者 https://www.devze.com 2023-01-08 18:57 出处:网络
I have the XML source file in SVG present like this: <svg> <g id=\'a001\' class=\'pools\'> <g id=\'b001\' class=\'pool\' name=\'Proc111\'>

I have the XML source file in SVG present like this:

<svg>
<g id='a001' class='pools'>
  <g id='b001' class='pool' name='Proc111'>
        <g id='c001' class='lane' name='User111' PoolID='b001'>
           <g id='d001' class='startevent' name='startevent111' LaneID='c001'></g>
      </g>   
        <g id='c002' class='lane' name='User222' PoolID='b001'>
            <g id='d002' class='gateway' name='gateway111' LaneID='c002'></g>
      &l开发者_如何转开发t;/g>
    </g>
     <g id='b002' class='pool' name='Proc222'>
       <g id='c003' class='lane' name=' customer ' PoolID=' b002'>
          <g id='d003' class='endevent' name='endevent111' LaneID='c003'>  </g>
      </g>
    </g>
</g>
<g id='a002' class='messageflows'/>

</svg>

i wanna tranform to XML target as following document:

<process id='a001' name='proc111'>
  <laneset>
   <lane name='User111'/>
   <lane name='User222'/>
  </laneset>
  <startevent id='d001' name='startevent111'/>
  <gateway id='d002' name='gateway111'/>
</process>

  <process id='a002' name='proc222'>
  <laneset>
   <lane name='customer'/>
  </laneset>
  <endevent id='d003' name='endevent111'/>
</process>

I had tried with some transformation but it was unsuccessfull...My solution is create the main template; and inside the main template(process template), I call for the other templates which are starteventtemplate, endeventtemplate, gateway template, etc. However, in the target document, I get all childrent which are present the same for each parent elements('process').

Because the real file is bigger than this example with a lot of elements...So, I've carried out the main problem to apply for the remaining elements.


Using the following stylesheet you can generate the desired output:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes" />
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <processes>
      <xsl:apply-templates />
    </processes>
  </xsl:template>

  <xsl:template match="g[@class='pool']" >
    <process>
      <laneset>
        <xsl:apply-templates mode="laneset" />
      </laneset>
      <xsl:apply-templates mode="startevent" />
      <xsl:apply-templates mode="gateway" />
    </process>
  </xsl:template>

  <xsl:template match="g[@class='lane']" mode="laneset">
    <lane name="{@name}"/>
  </xsl:template>

  <xsl:template match="g[@class='startevent']" mode="startevent">
    <startevent id="{@id}" name="{@name}"/>
  </xsl:template>

  <xsl:template match="g[@class='gateway']" mode="gateway">
    <gateway id="{@id}" name="{@name}"/>
  </xsl:template>

</xsl:stylesheet>


This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="g[@class='pools']">
        <processes>
            <xsl:apply-templates />
        </processes>
    </xsl:template>
    <xsl:template match="g[@class='pool']" priority="1">
        <process id="{@id}" name="{@name}">
            <laneset>
                <xsl:apply-templates/>
            </laneset>
            <xsl:apply-templates select="*/*"/>
        </process>
    </xsl:template>
    <xsl:template match="g/g">
        <xsl:element name="{@class}">
            <xsl:copy-of select="@id[../@class != 'lane']|@name"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Output:

<processes>
    <process id="b001" name="Proc111">
        <laneset>
            <lane name="User111" />
            <lane name="User222" />
        </laneset>
        <startevent id="d001" name="startevent111" />
        <gateway id="d002" name="gateway111" />
    </process>
    <process id="b002" name="Proc222">
        <laneset>
            <lane name=" customer " />
        </laneset>
        <endevent id="d003" name="endevent111" />
    </process>
</processes>

EDIT: compacting code.

0

精彩评论

暂无评论...
验证码 换一张
取 消