[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Help needed in recursively converting the flat xml to a heirarchical XML


Subject: Re: [xsl] Help needed in recursively converting the flat xml to a heirarchical XML
From: "Sridhar Anupindi" <sridharanupindi@xxxxxxxxxxx>
Date: Tue, 30 Mar 2004 00:45:54 +0530

Hi Wendell,

I definitely agree with you and far the past two days i have been studying and working the same to get my desired output, and i was able to go forward a little bit.

Here are the list of things you have asked for:

Input XML:

<processXML><process-definition>
<name>Testing</name>
<description>Testing</description>
<start-state name="Start"><description>Start</description>
<transition to="WA0-Creator" />
</start-state>
<end-state name="Finish" />
<activity-state name="WA0-Creator"><description>WA0-Creator</description>
<transition to="OR-WA0-Creator" />
</activity-state>
<decision name="OR-WA0-Creator"><transition to="WA1" />
<transition to="AND-Split11" />
</decision>
<activity-state name="WA1"><description>WA1</description>
<transition to="Finish" />
</activity-state>
<concurrent-block>
<fork name="AND-Split11"><transition to="WA2A" />
<transition to="WA2B" />
</fork>
</concurrent-block>
<concurrent-block>
<fork name="AND-Split19"><transition to="WA5B" />
<transition to="WA5A" />
</fork>
</concurrent-block>
<activity-state name="WA5B"><description>WA5B</description>
<transition to="OR-Join71" />
</activity-state>
<activity-state name="WA5A"><description>WA5A</description>
<transition to="OR-Join71" />
</activity-state>
<join name="OR-Join71"><transition to="AND-Join58" />
</join>
<activity-state name="WA2A"><description>WA2A</description>
<transition to="AND-Split19" />
</activity-state>
<activity-state name="WA2B"><description>WA2B</description>
<transition to="AND-Join58" />
</activity-state>
<join name="AND-Join58"><transition to="WA2C" />
</join>
<activity-state name="WA2C"><description>WA2C</description>
<transition to="Finish" />
</activity-state>
</process-definition>
</processXML>

Output XML:

<processXML><process-definition>
<name>Testing</name>
<description>Testing</description>
<start-state name="Start"><description>Start</description>
<transition to="WA0-Creator" />
</start-state>
<end-state name="Finish" />
<activity-state name="WA0-Creator"><description>WA0-Creator</description>
<transition to="OR-WA0-Creator" />
</activity-state>
<decision name="OR-WA0-Creator"><transition to="WA1" />
<transition to="AND-Split11" />
</decision>
<activity-state name="WA1"><description>WA1</description>
<transition to="Finish" />
</activity-state>
<concurrent-block>
<fork name="AND-Split11"><transition to="WA2A" />
<transition to="WA2B" />
</fork>
<activity-state name="WA2A"><description>WA2A</description>
<transition to="AND-Join58" />
</activity-state>
<activity-state name="WA2B"><description>WA2B</description>
<transition to="AND-Join58" />
</activity-state>
<join name="AND-Join58"><transition to="WA2C" />
</join>
<concurrent-block>
<fork name="AND-Split19"><transition to="WA5B" />
<transition to="WA5A" />
</fork>
<activity-state name="WA5B"><description>WA5B</description>
<transition to="OR-Join71" />
</activity-state>
<activity-state name="WA5A"><description>WA5A</description>
<transition to="OR-Join71" />
</activity-state>
<join name="OR-Join71"><transition to="AND-Join58" />
</join>
</concurrent-block>
</concurrent-block>

<activity-state name="WA2C"><description>WA2C</description>
<transition to="Finish" />
</activity-state>
</process-definition>
</processXML>

Rules :

1. As you can see my input xml consists of
<concurrent-block/fork/transition> nodes which has attribute value
(@to). I have to look for all the <activity-state> nodes with the
same value in (@name), and if matched, move the <activity-state> to
<concurrent-block>, else I have retain them at the same place.

2. Secondly, inturn the <activity-state> also has <transition> node
which can have values related to another <activity-state> node
and/or <join> node or <concurrent-block> node. If any matches are
found then I have to move all of them to the <concurrent-block>
node.

3. If a <concurrent-block> node is found in the second step, that
has to have all the related <activity-state> nodes, <join> nodes
etc., with in its block.

4. One or More <activity-state> nodes will refer to the same <join>
node but I need only one occurance of the <join> irrespective of
how many <activity-state> nodes are refering it. The basic rule is
"Each <concurrent-block> should contain only one <join> node."

Style Sheet:

<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet exclude-result-prefixes="fo ms" version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="urn:ms-xsl" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/> <xsl:template match="/ |node()"><xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy></xsl:template>
<xsl:template match="@*">
<xsl:copy>
</xsl:copy>
</xsl:template>


<xsl:key name="act-name" match="/processXML/process-definition/activity-state" use="@name"/>
<xsl:key name="jname" match="/processXML/process-definition/join" use="@name"/>
<xsl:key name="con-name" match="/processXML/process-definition/concurrent-block" use="fork/@name"/>


<xsl:template match="/processXML/process-definition/concurrent-block">
<xsl:copy>
<!-- To Copy Concurrent-Blocks -->
<xsl:for-each select="fork">
<xsl:variable name="forkname" select="@name"/>
<xsl:apply-templates select="/processXML/process-definition/concurrent-block/fork[@name=$forkname]"/>
<xsl:for-each select="transition">
<xsl:variable name="transname"><xsl:value-of select="@to"/></xsl:variable>
<xsl:copy-of select="key('act-name', $transname)"/>
<xsl:for-each select="key('act-name', $transname)">
<xsl:variable name="joinname" select="transition[1]/@to"/>
<xsl:copy-of select="key('jname', $joinname)"/>
<xsl:copy-of select="key('con-name', $joinname)"/>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>


The results so far I acheived:

1. I am able to copy the <activity-state>, <concurrent-block>, <join> nodes to the respective locations. But the problem is there are still at the source location. I need to work on removing them too.
2. Though I am able to copy the <concurrent-block> in case of nested, the problem here is only the <concuurent-block> does not contain any <activity-state>/<join>/<concuurent-block> nodes. You can see that when you transform the input xml with my xsl.
3.The number of join nodes copied are based on the no. of <activity-state> nodes refering to them, but my intention is to copy only one join node.. since according to the rules a fork can contain only join.
4.Since the example given here is fairly a simple one, but inside the <concurrent-block> an <activity-state> can have a transition to another <activity-state>/<decision>/<concurrent-block> multiple times and the last <activity-state> will have a transition to a <join>. This code I have written will only go thru the entire list only once and populates the xml.


These are the issues right now I am dealing with. May be you can guide me in achieving the desired results. I know I am missing some key point in my approach but I am not able to figure it out.
Is this approach correct in solving my problem?


I really appreciate your help.

Thanks

Sridhar Anupindi

From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Help needed in recursively converting the flat xml to a heirarchical XML
Date: Mon, 29 Mar 2004 13:10:34 -0500


Sridhar,

Thanks for the update.

Clarification of the spec is a critical first step, but you need to take it farther than that. Sure, given input and a complete spec of the problem, someone on this list could take a few minutes or an hour or two to write and test your stylesheet, but is that what you really want? (It might help hone your skills at, erm, finding solutions, but it wouldn't really help you learn XSLT, would it?) If you want our help with your XSLT, you really need to provide us with some to work with.

Could you show us your XSLT stylesheet so far, or if you have none, could you suggest where you are getting stuck in creating one?

======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================



Thanks

Sridhar Anupindi
(510)-390-3538

_________________________________________________________________
Contact brides & grooms FREE! http://www.shaadi.com/ptnr.php?ptnr=hmltag Only on www.shaadi.com. Register now!



Current Thread
Keywords