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 March 27th, 2006, 06:03 AM
Authorized User
 
Join Date: Feb 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Inorder traversal how to

Hi all,

I have some problem in selecting the nodes by using the in-order traversal.

Here is my xml

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <project>
        <project_id>100</project_id>
        <parent_id>100</parent_id>
        <name>Project 1</name>
    </project>
    <project>
        <project_id>104</project_id>
        <parent_id>102</parent_id>
        <name>Phase 4.2</name>
    </project>
        <project>
        <project_id>105</project_id>
        <parent_id>100</parent_id>
        <name>release 1</name>
    </project>

    <project>
        <project_id>101</project_id>
        <parent_id>100</parent_id>
        <name>release 1</name>
    </project>
    <project>
        <project_id>102</project_id>
        <parent_id>100</parent_id>
        <name>Phase 4</name>
    </project>
    <project>
        <project_id>103</project_id>
        <parent_id>102</parent_id>
        <name>Phase 4.1</name>
    </project>
</root>


and my xslt is

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="root">
        <xsl:apply-templates select="project"></xsl:apply-templates>
    </xsl:template>

    <xsl:template match="project">
        <xsl:value-of select="project_id"></xsl:value-of>
        <xsl:variable name="pid" select="project_id"/>
        <xsl:apply-templates select="/root/project[$pid=current()]"></xsl:apply-templates>
        <xsl:text>#xa;</xsl:text>
    </xsl:template>
</xsl:stylesheet>


The output that i want to get is

100
105
101
102
104
103

But I am not able to traverse the tree and iam getting output as

100
104
105
101
102
103

I am stuck out here. Please any one help me.

__________________
Regards,
dsekar_nat
 
Old March 27th, 2006, 06:13 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It's a bit hard to see what's going on because the XML you posted isn't well-formed.

I would expect your "root" template not to process all the child projects, but only the one that you know to be a top-level project. And your code for processing a project by finding its logical children looks quite wrong:

<xsl:apply-templates select="/root/project[$pid=current()]">

I would have expected to see

<xsl:apply-templates select="/root/project[parent_id=$pid]">

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 27th, 2006, 08:45 AM
Authorized User
 
Join Date: Feb 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi kay,

Thanks

Its working fine.

This is the xsl template I have used.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:variable name="projects" select="/root/project"/>
    <xsl:template match="root">
        <xsl:for-each select="project">
            <xsl:if test="project_id = parent_id">
                <xsl:apply-templates select="."/>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="project">
        <xsl:value-of select="project_id"/>
        <xsl:variable name="pi" select="project_id"/>
        <xsl:apply-templates select="/root/project[parent_id = $pi and project_id != $pi]"></xsl:apply-templates>
        <xsl:text>#xa;</xsl:text>
    </xsl:template>
</xsl:stylesheet>


 
Old March 27th, 2006, 09:16 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It would be better coding style, I think, to replace

<xsl:for-each select="project">
            <xsl:if test="project_id = parent_id">
                <xsl:apply-templates select="."/>
            </xsl:if>
        </xsl:for-each>


with

<xsl:apply-templates select="project[project_id = parent_id]"/>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference









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