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 October 17th, 2003, 07:28 AM
Registered User
 
Join Date: Oct 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default How do I do this in XSLT?

I have s tricky task to do in XSLT

In my source file I have three tables
a part_list, a link_list and an entity_list as below
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <part_list>
        <part>
            <id>1</id>
        </part>
        <part>
            <id>2</id>
        </part>
        <part>
            <id>3</id>
        </part>
    </part_list>
    <link_list>
        <link>
            <id>1</id>
            <parent_part>1</parent_part>
            <child_part>2</child_part>
            <mirrored>0</mirrored>
        </link>
        <link>
            <id>2</id>
            <parent_part>1</parent_part>
            <child_part>2</child_part>
            <mirrored>1</mirrored>
        </link>
        <link>
            <id>3</id>
            <parent_part>2</parent_part>
            <child_part>3</child_part>
            <mirrored>0</mirrored>
        </link>
    </link_list>
    <entity_list>
        <entity>
            <id>1</id>
            <part>3</part>
        </entity>
    </entity_list>
</root>

I transform this data into a tree as below

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <part id="1" mirrored="0">
        <link id="1">
            <part id="2" mirrored="0">
                <link id="3">
                    <part id="3" mirrored="0">
                        <entity id="1" mirrored="0"/>
                    </part>
                </link>
            </part>
        </link>
        <link id="2">
            <part id="2" mirrored="1">
                <link id="3">
                    <part id="3" mirrored="1">
                        <entity id="1" mirrored="1"/>
                    </part>
                </link>
            </part>
        </link>
    </part>
</root>


using the following XSLT
<?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="/">
        <xsl:element name="root">
            <xsl:call-template name="part">
                <xsl:with-param name="part" select="//root/part_list/part[1]"/>
                <xsl:with-param name="mirrored" select="0"/>
            </xsl:call-template>
        </xsl:element>
    </xsl:template>

    <xsl:template name="part">
        <xsl:param name="part"/>
        <xsl:param name="mirrored"/>
        <xsl:variable name="id" select="$part/id"/>
        <xsl:element name="part">
            <xsl:attribute name="id"> <xsl:value-of select="$id"/></xsl:attribute>
            <xsl:attribute name="mirrored"><xsl:value-of select="$mirrored"/></xsl:attribute>
            <xsl:for-each select="//root/link_list/link[parent_part=$id]">
                <xsl:call-template name="link">
                    <xsl:with-param name="link" select="."/>
                    <xsl:with-param name="parent_mirrored" select="$mirrored"/>
                </xsl:call-template>
            </xsl:for-each>
            <xsl:for-each select="//root/entity_list/entity[part=$id]">
                <xsl:call-template name="entity">
                    <xsl:with-param name="entity" select="."/>
                    <xsl:with-param name="mirrored" select="$mirrored"/>
                </xsl:call-template>
            </xsl:for-each>
        </xsl:element >
    </xsl:template>

    <xsl:template name="link">
        <xsl:param name="link"/>
        <xsl:param name="parent_mirrored"/>
        <xsl:variable name="id" select="$link/id"/>
        <xsl:variable name="mirrored">
            <xsl:choose>
                <xsl:when test="$link/mirrored=$parent_mirrored"><xsl:value-of select="0"/></xsl:when>
                <xsl:otherwise> <xsl:value-of select="1"/></xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:element name="link">
            <xsl:attribute name="id"> <xsl:value-of select="$id"/></xsl:attribute>
            <xsl:for-each select="//root/part_list/part[id=$link/child_part]">
                <xsl:call-template name="part">
                    <xsl:with-param name="part" select="."/>
                    <xsl:with-param name="mirrored" select="$mirrored"/>
                </xsl:call-template>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>

    <xsl:template name="entity">
        <xsl:param name="entity"/>
        <xsl:param name="mirrored"/>
        <xsl:variable name="id" select="$entity/id"/>
        <xsl:element name="entity">
            <xsl:attribute name="id"><xsl:value-of select="$id"/></xsl:attribute>
            <xsl:attribute name="mirrored"><xsl:value-of select="$mirrored"/></xsl:attribute>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

HERE IS MY PROBLEM
For every unique <entity> element (unique as regarding @id and @mirrored) I need to create a create a new table in my output like this

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <part id="1" mirrored="0">
        <link id="1">
            <part id="2" mirrored="0">
                <link id="3">
                    <part id="3" mirrored="0">
                        <entity id="1" mirrored="0"/>
                    </part>
                </link>
            </part>
        </link>
        <link id="2">
            <part id="2" mirrored="1">
                <link id="3">
                    <part id="3" mirrored="1">
                        <entity id="1" mirrored="1"/>
                    </part>
                </link>
            </part>
        </link>
    </part>
    <entities>
        <entity id="1" mirrored="0"/>
        <entity id="1" mirrored="1"/>
    </entities>
</root>

It is as if I need to be able select unique node-sets from the my output so far.
How do I do this????????


 
Old October 17th, 2003, 10:44 AM
Registered User
 
Join Date: Oct 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have just solved this myself


I use the following javascript function like this each time i output a <element> element

Code:
var prototypes = new Array();
function is_new_prototype(key) {
    if (typeof prototypes[key] == "undefined") {
    prototypes[key] = 1;
    return 1;
    }
    return 0;
};
I test to see if this is the first time the element has been output
with something like the following xsl

Code:
<xsl:variable name="key" select="concat(@id, '_', @mirrored)"/>
<xsl:if test="js:is_new_element($key)">
    ....
</xsl:if>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating XSLT with XSLT stonis XSLT 3 April 1st, 2008 08:17 PM
General XSLT Questions in the XSLT Forum jminatel BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 0 March 31st, 2008 07:50 PM
Can XSLT read DTD/schema and Generate XSLT.. ROCXY XSLT 1 November 6th, 2006 09:39 AM
dynamic xslt -> xslt creation namespace problem jkmyoung XSLT 2 July 15th, 2006 12:42 AM
xslt with an xslt outputfile alleycat XSLT 4 February 20th, 2006 09:56 AM





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