You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Benoit Cerrina <be...@writeme.com> on 2000/10/23 08:52:29 UTC

Fw: import traversal

Hi,
remember when I said I'd look into implementing a <break/> extension element or some such, well you've made me change my mind, however, here is a simplified version of my actual problem, if any of you has any idea on how to look at it...
Benoit
----- Original Message ----- 
From: Benoit Cerrina 
To: xsl-list@mulberrytech.com 
Sent: Sunday, October 22, 2000 9:01 PM
Subject: import traversal


Hi,
here is my problem:
I have some xml documents with the equivalent of an import clause in java, essentially it could look like this 
----doc a-----------
<?xml version="1.0" encoding="UTF-8"?>
<doc name="a">
    <import name="b"/>
    <import name="c"/>
 <object id="1" ref="2" payload="A"/>
</doc>
----end doc a-----------

----doc b-----------
<?xml version="1.0" encoding="UTF-8"?>
<doc name="b">
 <import name="a"/>
 <object id="2" ref="3" payload="B"/> 
 <object id="3" payload="C"/> 
</doc>
----end doc b-----------

----doc c-----------
<?xml version="1.0" encoding="UTF-8"?>
<doc name="c">
    <import name="b"/>
</doc>
----end doc c-----------
So we have an import tree which looks like:
a---->b---->a
|---->c---->b---->a

Now I'm processing the document "a"
I'd like to get:
<object id=1>
 ABC
</object>
or in english each time I encounter an object I need to print out it's payload and the payload of its reference and its reference's reference...
My problem is that I know how to do a depth first transversal of the import graph of document, I even know how to avoid problems with the cycle which appears due to the import of a in b, however I don't know how to deal with the fact that both branches have a part in common. This is actually the same problem as with multiinheritance in c++ and the losange pattern.
All my efforts have yielded the following result:
<object id=1>
 ABCBC
</object>


Benoit

PS: 
this is what my stylesheet looks like:

------importWalker.xsl-------
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 <xsl:output method="xml" indent="yes"/>
 <!--main template-->
 <xsl:template match="doc">
  <xsl:apply-templates select="object"/>
 </xsl:template>

 <xsl:template match="object">
  <object id="{@id}">
   <xsl:apply-templates select="." mode="payload"/>
  </object>
 </xsl:template>


 <xsl:template match="object" mode="payload">
  <!--passthrough parameter to the walker-->
  <xsl:param name="visitedDocs" select="/.."/>
   <xsl:value-of select="@payload"/> 
   <xsl:if test="@ref">
    <xsl:call-template name="importWalker">
     <xsl:with-param name="visitedDocs" select="$visitedDocs"/>
     <xsl:with-param name="target" select="@ref"/>
    </xsl:call-template>
   </xsl:if>
 </xsl:template>
 
 <xsl:template name="importWalker">
  <xsl:param name="visitedDocs" select="/.."/>
  <xsl:param name="target"/>
  <xsl:variable name="newVisitedDocs" select="$visitedDocs | /doc/@name"/>
  <xsl:variable name="targetObj" select="//object[@id = $target]"/>
  <xsl:choose>
   <!--first test if the object is in this document I know // is ugly in truth I use key-->
   <xsl:when test="$targetObj">
    <!--found it print payloads and recurse-->
    <xsl:apply-templates select="$targetObj" mode="payload">
     <xsl:with-param name="visitedDocs" select="$newVisitedDocs"/>
    </xsl:apply-templates>
   </xsl:when>
   <xsl:otherwise>
    <!--iterate and recurse other all imports-->
    <xsl:for-each select="document(/doc/import/@name, .)">
     <!--check for import cycles-->
     <xsl:variable name="docName" select="/doc/@name"/>
     <xsl:if test="not($newVisitedDocs[string(.) = string($docName)])">
      <xsl:call-template name="importWalker">
       <xsl:with-param name="target" select="$target"/>
       <xsl:with-param name="visitedDocs" select="$newVisitedDocs"/>
      </xsl:call-template>
     </xsl:if>
    </xsl:for-each>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>
------end importWalker.xsl-------


Re: Fw: import traversal

Posted by Gary L Peskin <ga...@firstech.com>.
Benoit Cerrina wrote:
> Once more sorry for the post in the wrong list.

This isn't the wrong list.  I just thought you'd get more people looking
at it over on XSL-List.

Gary

Re: Fw: import traversal

Posted by Benoit Cerrina <be...@writeme.com>.
Hi Gary,
your right, it wasn't directly xalan related, however I tried first to send
it to xsl-list but the mails didn't seem to reach the list, they ended there
(in three examplar since I kept trying to find out why they would bounce)
but a lot later than usual and I think I got an explanation in a recent mail
from the owner of the list saying he had some problem with the list going
into a loop and he was approving the mails individually which explains the
lateness.

Once more sorry for the post in the wrong list.  The one part which may
concern you is that I've grown so frustrated with the whole thing that I've
been thinking more and more into adding this <break/> element. However I've
been told it looked to procedural and shouldn't be needed.  One thing
though, what I really need is more than a c++ or java break, rather a prolog
cut (! in prolog) I think every body will agree that prolog is NOT
procedural.  The thing though is that if it was necessary in prolog it
probably is needed elsewhere.  At first I was looking at it for performance
reason, now it is just to get a consistant result
Benoit
----- Original Message -----
From: "Gary L Peskin" <ga...@firstech.com>
To: <xa...@xml.apache.org>
Sent: Tuesday, October 24, 2000 5:33 AM
Subject: Re: Fw: import traversal


> Benoit Cerrina@writeme.com wrote:
> >Hi,
> >remember when I said I'd look into implementing a <break/> extension
element or
> >some such, well you've made me change my mind, however, here is a
simplified version
> >of my actual problem, if any of you has any idea on how to look at it...
> >Benoit
>
> Benoit --
>
> I haven't had a change to look at your challenge here but you might want
> to forward this to XSL-List where a lot more eyes will see it.  Since
> you're looking for a straight XSL solution, you will probably get a
> faster response there.
>
> Gary


Re: Fw: import traversal

Posted by Gary L Peskin <ga...@firstech.com>.
Benoit Cerrina@writeme.com wrote:
>Hi,
>remember when I said I'd look into implementing a <break/> extension element or
>some such, well you've made me change my mind, however, here is a simplified version
>of my actual problem, if any of you has any idea on how to look at it...
>Benoit

Benoit --

I haven't had a change to look at your challenge here but you might want
to forward this to XSL-List where a lot more eyes will see it.  Since
you're looking for a straight XSL solution, you will probably get a
faster response there.

Gary