Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 16th, 2007, 08:04 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default Grouping to create Org chart

Hi All,

I am little stumped on how to to dynamicly create an Origination Chart. To start I have an xstl that creates an XML file:

Code:
<org>
    <position reportsto="Director of Engineering">Manager Publications</position>
    <position reportsto="Manager Technical Publications">Supervisor Publications – Development </position>
    <position reportsto="Manager Publications">Supervisor Publications – Services</position>
    <position reportsto="Supervisor Publications – Development">Technical Writer</position>
    <position reportsto="Supervisor Publications –  Services">Publications Specialist</position>
    <position reportsto="VP of Operations">Director of Quality</position>
    <position reportsto="VP of Operations">Director of Engineering</position>
    <position reportsto="Director of Engineering">Manager Publications</position>

    <position reportsto="VP of Operations">Director of Quality</position>
    <position reportsto="Director of Quality">Manager Quality Assurance</position>
</org>

here is my XSLT which groups and sorts, but does not give me the my desired result:

Code:
<xsl:for-each select="$XML">
                    <xsl:for-each select="org">
                        <xsl:for-each-group select="position/@reportsto" group-by=".">
                            <xsl:sort/>
                            <xsl:value-of select="."/>
                            <xsl:for-each-group select="current-group()" group-by="..">
                                [list]
                                    <xsl:value-of select=".."/>
                                </ul>
                                <br/>
                            </xsl:for-each-group>
                        </xsl:for-each-group>
                    </xsl:for-each>
                </xsl:for-each>


Here is the desired result in HTML (XSLT 2.0):

Code:
VP of Operations 
       Director of Quality
          Manager Quality Assurance 
       Director of Engineering
          Manager Publications
              Supervisor Publications – Development 
                  Technical Writer 
              Supervisor Publications – Services 
                  Publications Specialist
I have a mental block and I hope that you can provide some help

Thanks!

 
Old February 16th, 2007, 08:38 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It's not actually a grouping problem. Write a recursive template that matches a node in the tree, outputs that node, searches the tree to find its logical children (those that report to the current node) and then does a recursive call (apply-templates works well) to process each of those logical children in turn.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 16th, 2007, 08:41 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Thank you Michael, I will give a try.



 
Old February 16th, 2007, 10:40 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Michael,

As I am attemping to try your suggestion, I keep coming accross a problem, I am not sure how to resolve. You suggested "searches the tree to find its logical children (those that report to the current node) ". I am not sure how. Here is my XSLT so far:
Code:
    <xsl:template match="/">
        <html>
            <head>
                <title/>
            </head>
            <body>
                <xsl:for-each select="$XML">
                    <xsl:for-each select="org">
                        <xsl:for-each select="//@reportsto">
                            <span>
                                <xsl:apply-templates select="."/>
                                <br/>
                            </span>
                        </xsl:for-each>
                    </xsl:for-each>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="position">
        <xsl:value-of select="."/>
    </xsl:template>
    <xsl:template match="@reportsto">
        <xsl:value-of select="string(.)"/>---->
        <xsl:for-each select="..">
            <xsl:apply-templates select="."/>
        </xsl:for-each>
    </xsl:template>

I guess my questions is, if I select all the "reportsto" which is an attribute of "position" how do i then search the entire XML tree for any matches?

Thank you for the help.

 
Old February 18th, 2007, 04:10 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Any suggestions?


 
Old February 18th, 2007, 05:31 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You want something like

<xsl:template match="position">
  <xsl:value-of select="."/>
  <xsl:apply-templates select="//position[.=current()/@reportsto]"/>
</xsl:template>

with some added formatting.

You can use keys to make it more efficient: you can always replace

select="//X[Y=Z]"

by

<xsl:key name="K" match="X" use="Y"/>

then

select="key('K', Z)"

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 19th, 2007, 08:08 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Thank you Michael, I will give it a try,








Similar Threads
Thread Thread Starter Forum Replies Last Post
Create The Mouse Over on the Pie chart Report Kali Charan Tripathi Reporting Services 0 October 9th, 2008 06:37 AM
how to create 3d pie chart Sunilmesu Pro Visual Basic 2005 1 April 30th, 2008 10:52 AM
Create Dynamic Organization chart subhasps8 ASP.NET 2.0 Professional 1 April 16th, 2008 03:55 AM
How to create an excell chart with ASP tiahvera Classic ASP Databases 1 August 3rd, 2007 06:39 PM
How to create a chart in ASP rekha_jsr Classic ASP Basics 1 December 9th, 2004 12:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.