xsl-list
[Top] [All Lists]

RE: Hierearchy navigation in XSL

2002-10-14 08:01:24
Given the sample xml, I think this will do what you want:

<xsl:template match="/masterdoc">
  <xsl:variable name="thisns">
    <xsl:if test="$ns">
      <xsl:value-of select="concat($ns,'.')"/>
    </xsl:if>
  </xsl:variable>
  <xsl:for-each select="class[starts-with(@namespace,$ns)]">
    <xsl:variable name="nsafter"
select="substring-before(concat(substring-after(@namespace,$thisns),'.'),'.'
)"/>
    <xsl:if
test="not(preceding-sibling::class[starts-with(@namespace,concat($thisns,$ns
after))])">
      <xsl:value-of select="$nsafter"/>
    </xsl:if>
    <xsl:text>
    </xsl:text>
  </xsl:for-each>
</xsl:template>

HTH,
David.
--
David McNally            Moody's Investors Service
Software Engineer        99 Church St, NY NY 10007 
David(_dot_)McNally(_at_)Moodys(_dot_)com            (212) 553-7475 

-----Original Message-----
From: Scott Bronson [mailto:bronson(_at_)rinspin(_dot_)com]
Sent: Monday, October 14, 2002 5:36 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Hierearchy navigation in XSL


Hello.  I've solved most of this problem, but the last tiny bit has me
stumped.  I'm hoping someone can tell me how to fix this.

Here's some input:


<masterdoc>
  <class name="Object"       namespace="System"/>
  <class name="Array"        namespace="System"/>
  <class name="ArrayList"    namespace="System.Collections"/>
  <class name="Comparer"     namespace="System.Collections"/>
  <class name="Grimey"       namespace="System.Collections.Overkill"/>
  <class name="Formatter"    
namespace="System.Runtime.Serialization"/>
  <class name="ObjectHandle" namespace="System.Runtime.Remoting"/>
  <class name="Garbage"      namespace="Other.SubAPI"/>
</masterdoc>


My script accepts a parameter that tells where it is in the 
hierarchy. 
It then outputs all immediate child nodes at that level in the
hierarchy.

Some examples: if we're at "System", the script should output
"Collections" and "Runtime".  If we're at "System.Collections", it
should output "Overkill".  If we're at "", it should output 
"System" and
"Other".  If at "Other", output "SubAPI".  Seems a fairly 
easy problem,
right?



Here's my script right now:


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
<xsl:output method="text"/>
<xsl:param name="ns"/>
<xsl:key name='uniq' match='class' use='@namespace'/>

<xsl:template match="/masterdoc">
  <xsl:for-each
select="class[generate-id()=generate-id(key('uniq',@namespace)[1])]">
    <xsl:sort select="@namespace"/>
    <xsl:choose>
      <xsl:when test="string-length($ns)=0">
        <xsl:value-of select="@namespace"/>
      </xsl:when>
      <xsl:when test="starts-with(@namespace,concat($ns,'.'))">
        <xsl:value-of
select="substring(@namespace,string-length($ns)+2)"/>
      </xsl:when>
    </xsl:choose>
<xsl:text>
</xsl:text>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>



Here are some example runs of this script:

xalan -Q -XSL samp1.xsl -IN sample.xml -PARAM ns 
"'System.Collections'"
Overkill

xalan -Q -XSL samp1.xsl -IN sample.xml -PARAM ns "'System'"
Collections
Collections.Overkill
Runtime.Remoting
Runtime.Serialization


The last one illustrates the problem.  It should have output only
"Collections" and "Runtime".

I picture the algorithm being something like:
   if(not(already copied string-before("Runtime.Remoting", "."))
      then copy string-before("Runtime.Remoting", ".") to output

I've tried various combinations of preceding, preceding sibling, and
position to try to figure out what I've already output.  I even tried
key() and using IDs to navigate the parent.  All has resulted in
frustration.

Does anyone have any idea of what I can do?  I'm out of ideas.

Thank you!

    - Scott





 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



---------------------------------------

The information contained in this e-mail message, and any attachment thereto, 
is confidential and may not be disclosed without our express permission.  If 
you are not the intended recipient or an employee or agent responsible for 
delivering this message to the intended recipient, you are hereby notified that 
you have received this message in error and that any review, dissemination, 
distribution or copying of this message, or any attachment thereto, in whole or 
in part, is strictly prohibited.  If you have received this message in error, 
please immediately notify us by telephone, fax or e-mail and delete the message 
and all of its attachments.  Thank you.

Every effort is made to keep our network free from viruses.  You should, 
however, review this e-mail message, as well as any attachment thereto, for 
viruses.  We take no responsibility and have no liability for any computer 
virus which may be transferred via this e-mail message.


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>