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 April 7th, 2005, 01:42 PM
Registered User
 
Join Date: Apr 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default am i doing this right?

greetings,

very new to xml/xslt. I was wondering if I might get the opinnions of some more seasoned developers on my approach for this specific task.

The end result of what I am trying to do is an HTML page that displays data from 2 xml documents. The structure of the xml docs are identical but the data is different.

Currently, I have 1 xml doc that looks like this:

Code:
<comparison>
     <data>data1.xml</data>
     <data>data2.xml</data>
</comparison>
I use this to transform an XSLT that creates references to my 2 xml docs (data1.xml and data2.xml) like so:


Code:
<xsl:variable name="data1_file_name" select="/comparison/data[1]" />
<xsl:variable name="data1" select="document($data1_file_name)" />
<xsl:variable name="data2_file_name" select="/comparison/data[2]" />
<xsl:variable name="data2" select="document($data2_file_name)" />

Then for each set of data from $data1 and $data2 I am displaying, I create a named template and pass it a parameter telling it to either display data from the appropriate xml source.

This is cery watered down, but it should get the point across.



Code:
<xsl:template match="/">
<html>
<head>
</head>
<body>
<table>

    <tr>
        <td>
            <xsl:call-template name="compare_guests">
                <xsl:with-param name="thisBase" select="$data1/party/guests" />
            </xsl:call-template>
        </td>
    </tr>

    <tr>
        <td>
            <xsl:call-template name="compare_guests">
                <xsl:with-param name="thisBase" select="$data2/party/guests" />
            </xsl:call-template>
        </td>
    </tr>
</table>

<xsl:template name="compare_guests">
    <xsl:param name="thisBase" />
    Total Invited: <xsl:value-of select="$thisBase/total" /><br />
    Confirmed: <xsl:value-of select="$thisBase/confirmed" /><br />
    Declined: <xsl:value-of select="$thisBase/declined" /><br />
</xsl:template>

The page contains several of these tables, each with their own template.

Is there a more standard or efficient way I should be going about this?


 
Old April 7th, 2005, 01:48 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can write a bit less code by using apply-templates. Instead of

<xsl:call-template name="compare_guests">
                <xsl:with-param name="thisBase" select="$data1/party/guests" />
            </xsl:call-template>

write

<xsl:apply-templates select="$data1/party/guests mode="compare-guests"/>

and instead of

<xsl:template name="compare_guests">
    <xsl:param name="thisBase" />
    Total Invited: <xsl:value-of select="$thisBase/total" /><br />
    Confirmed: <xsl:value-of select="$thisBase/confirmed" /><br />
    Declined: <xsl:value-of select="$thisBase/declined" /><br />
</xsl:template>

write

<xsl:template match="guests" mode="compare_guests">
    Total Invited: <xsl:value-of select="total" /><br />
    Confirmed: <xsl:value-of select="confirmed" /><br />
    Declined: <xsl:value-of select="declined" /><br />
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 7th, 2005, 02:16 PM
Registered User
 
Join Date: Apr 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Awesome! I thought it had something to do with mode attribute but couldn't quite grasp it.

Is there a "better" resource for learning this stuff than w3schools.com?










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