xsl-list
[Top] [All Lists]

RE: [xsl] xsl transormation from flat tree to hierarchical tree

2012-03-26 10:53:39
Andreas,

Switching from call-template to apply-templates would have been my first 
suggestion, but Wendell covered that already. :) I would say, go one step 
further and use a key:

<xsl:key name="child-states" match="state" use="@parent" />
<xsl:template match="state" mode="make-node">
   <node id="{@name}" label="{@name}" fontname="Arial" fontsize="9">
     <xsl:apply-templates mode="make-node" select="key('child-states', @name)" 
/>
   </node>
</xsl:template>

This ends up being a lot faster - you don't have to recalculate //state every 
time, and you don't have to search through each of those nodes to get the ones 
that match @parent. A <xsl:key> specifies that the processor should keep a 
named index for certain nodes. You specify the name of the index 
(name="child-state"), which nodes it should keep in the index (match="state"), 
and the index property (use="@parent"). Then while it's initially parsing the 
source XML tree, if it finds a node that matches, it evaluates the index 
property XPath with the matched node as context, and adds a reference to the 
named index using the evaluated value as the key. Then when you want to get all 
the nodes matching a certain key value, you call key('key-name', key-value), 
and it instantly retrieves all indexed nodes at that key value.

So for the example above, in the apply-templates, you're applying to all state 
nodes (at any level) that have @parent = current()/@name. It ends up being the 
exact same as what Wendell's code below is doing, but a lot faster and much 
cleaner.

~ Scott


-----Original Message-----
From: Wendell Piez [mailto:wapiez(_at_)mulberrytech(_dot_)com] 
Sent: Monday, March 26, 2012 10:35 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] xsl transormation from flat tree to hierarchical tree

Dear Andreas,

First, your code would also be easier to understand if it weren't so verbose. 
Keep in mind that these two are exactly equivalent:

<xsl:template name="printStateTree"
   xmlns:dotml="http://www.martin-loetzsch.de/DOTML";>
   <xsl:param name="curState"/>
   <xsl:element name="node">
     <xsl:attribute name="id">
       <xsl:value-of select="$curState/@name"/>
     </xsl:attribute>
     <xsl:attribute name="label">
       <xsl:value-of select="$curState/@name"/>
     </xsl:attribute>
     <xsl:attribute name="fontname">Arial</xsl:attribute>
     <xsl:attribute name="fontsize">9</xsl:attribute>
     <xsl:for-each select="//state[@parent = $curState/@name]">
       <xsl:call-template name="printStateTree">
         <xsl:with-param name="curState" select="."/>
       </xsl:call-template>
     </xsl:for-each>
   </xsl:element>
</xsl:template>

<xsl:template name="printStateTree"
   xmlns:dotml="http://www.martin-loetzsch.de/DOTML";>
   <xsl:param name="curState"/>
   <node id="{$curState/@name}" label="{$curState/@name}"
      fontname="Arial" fontsize="9">
     <xsl:for-each select="//state[@parent = $curState/@name]">
       <xsl:call-template name="printStateTree">
         <xsl:with-param name="curState" select="."/>
       </xsl:call-template>
     </xsl:for-each>
   </node>
</xsl:template>

Second, this is easily refactored into templates, which means you don't have to 
pass a node in for context:

<xsl:template match="state" mode="make-node">
   <node id="{@name}" label="{@name}"
     fontname="Arial" fontsize="9">
     <xsl:apply-templates mode="make-node"
       select="//state[@parent = current()/@name]" />
   </node>
</xsl:template>

(This does the same thing as your code, including using the state/@name twice, 
once for the node/@id and one for the node/@label. So this will have to be 
fixed.)

If you don't understand how this works, it's time to study up on templates, 
template matching and the XSLT processing model.

Cheers,
Wendell

On 3/24/2012 3:15 PM, Mukul Gandhi wrote:
Hi Andreas,
    My apologies for a delayed response (since this question was directed to 
me).

I think, I don't have a concrete suggestion for your new question 
below, since it seems to me that this question is very specific to 
your application domain.

I'll leave it to anyone else to answer this, or I wish you good luck 
to discover the answer yourself.

On Sun, Mar 18, 2012 at 10:30 AM, Andreas 
Volz<lists(_at_)brachttal(_dot_)net>  wrote:
Am Sat, 10 Mar 2012 16:00:47 +0530 schrieb Mukul Gandhi:

Hi Mukul,

thanks for that hints! I never used that call-templates before. After 
some experiments I got it somehow running in my xsl:

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

<xsl:template name="printStateTree" 
xmlns:dotml="http://www.martin-loetzsch.de/DOTML";>
     <xsl:param name="curState"/>

      <xsl:element name="node">

          <xsl:attribute name="id"><xsl:value-of select="$curState/@name"/>
          </xsl:attribute>

          <xsl:attribute name="label"><xsl:value-of 
select="$curState/@name"/>
          </xsl:attribute>

          <xsl:attribute name="fontname">Arial</xsl:attribute>

          <xsl:attribute name="fontsize">9</xsl:attribute>

          <xsl:for-each select="//state[@parent = $curState/@name]">

              <xsl:call-template name="printStateTree">
                  <xsl:with-param name="curState" select="."/>
              </xsl:call-template>

         </xsl:for-each>

      </xsl:element>

</xsl:template>

<xsl:template match="/">
    <dotml:graph file-name="stateval" 
xmlns:dotml="http://www.martin-loetzsch.de/DOTML";
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
      xsi:schemaLocation="http://www.martin-loetzsch.de/DotML 
../dotml-1.3/dotml-1.3.xsd" label="stateval" fontcolor="#0000A0" 
fontname="Arial Bold" margin="0.2,0.1" ranksep="0.2" nodesep="0.5"
      bgcolor="#F0F0FF" fontsize="13" style="dashed">

        <xsl:for-each select="stateval/states">

          <xsl:call-template name="printStateTree">
               <xsl:with-param name="curState" select="state[not(@parent)]"/>
          </xsl:call-template>

        </xsl:for-each>

        <xsl:for-each select="stateval/transitions/transition">
            <dotml:edge>
                <xsl:attribute name="from"><xsl:value-of select="@from"/>
                </xsl:attribute>

                <xsl:attribute name="to"><xsl:value-of select="@to"/>
                </xsl:attribute>

                <xsl:attribute name="label"><xsl:value-of select="@event"/>
                </xsl:attribute>

                <xsl:attribute name="fontname">Arial</xsl:attribute>

                <xsl:attribute name="fontsize">7</xsl:attribute>
            </dotml:edge>
        </xsl:for-each>

    </dotml:graph>
</xsl:template>

<xsl:template match="*|text()|@*">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>


</xsl:stylesheet>

But the output is still not as I need it:

<?xml version="1.0"?>
<dotml:graph xmlns:dotml="http://www.martin-loetzsch.de/DOTML"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; file-name="stateval" 
xsi:schemaLocation="http://www.martin-loetzsch.de/DotML 
../dotml-1.3/dotml-1.3.xsd" label="stateval" fontcolor="#0000A0" 
fontname="Arial Bold" margin="0.2,0.1" ranksep="0.2" nodesep="0.5" 
bgcolor="#F0F0FF" fontsize="13" style="dashed">
    <node id="Root" label="Root" fontname="Arial" fontsize="9">
        <node id="Initial" label="Initial" fontname="Arial" fontsize="9" />
        <node id="Final" label="Final" fontname="Arial" fontsize="9" />
        <node id="Main" label="Main" fontname="Arial" fontsize="9" />
        <node id="Browser" label="Browser" fontname="Arial" fontsize="9" />
        <node id="Settings" label="Settings" fontname="Arial" fontsize="9" />
        <node id="EMail_Compound" label="EMail_Compound" fontname="Arial" 
fontsize="9">
            <node id="EMail_Compound_Initial" label="EMail_Compound_Initial" 
fontname="Arial" fontsize="9" />
            <node id="EMail_Compound_History" label="EMail_Compound_History" 
fontname="Arial" fontsize="9" />
            <node id="EMail_Read" label="EMail_Read" fontname="Arial" 
fontsize="9" />
            <node id="EMail_Compose" label="EMail_Compose" fontname="Arial" 
fontsize="9" />
        </node>
    </node>
    <dotml:edge from="Initial" to="Main" label="" fontname="Arial" 
fontsize="7" />
    <dotml:edge from="Root" to="Final" label="EXIT" fontname="Arial" 
fontsize="7" />
    <dotml:edge from="Root" to="Main" label="MAIN" fontname="Arial" 
fontsize="7" />
    <dotml:edge from="Root" to="Browser" label="BROWSER" fontname="Arial" 
fontsize="7" />
    <dotml:edge from="Root" to="Settings" label="SETTINGS" fontname="Arial" 
fontsize="7" />
    <dotml:edge from="Root" to="EMail_Compound" label="EMAIL" 
fontname="Arial" fontsize="7" />
    <dotml:edge from="EMail_Compound" to="EMail_Compound_Initial" label="" 
fontname="Arial" fontsize="7" />
    <dotml:edge from="EMail_Compound_Initial" to="EMail_Compound_History" 
label="" fontname="Arial" fontsize="7" />
    <dotml:edge from="EMail_Compound_History" to="EMail_Read" label="" 
fontname="Arial" fontsize="7" />
    <dotml:edge from="EMail_Compound" to="EMail_Compose" 
label="EMAIL_COMPOSE" fontname="Arial" fontsize="7" />
    <dotml:edge from="EMail_Compound" to="EMail_Read" 
label="EMAIL_READ" fontname="Arial" fontsize="7" /> </dotml:graph>

It has now correct transformation into a parent relation, but dotml needs 
format like this:

<record>
  <node         id="10" label="left"/>
  <node         id="11" label="middle"/>
  <node         id="12" label="right"/>
</record>

<record>
  <node         id="20" label="one"/>
  <node         id="21" label="two"/>
</record>

I tried to learn more about xsl by reading w3schools docu, but I 
wasn't able to get exact that result. So I like to have this:

Could you maybe give me please another hint?

regards
  Andreas





--
======================================================================
Wendell Piez                            
mailto:wapiez(_at_)mulberrytech(_dot_)com
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 
======================================================================

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--