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 July 24th, 2006, 06:32 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You say: So I could write this

<tr>
    <td><xsl:value-of select ="soccerteam/team/teamid"/></td>
    <td><xsl:value-of select="soccerteam/team/@name"/></td>
    <td><xsl:value-of select="soccerteam/team/trainer"/></td>
    <td>...</td>
</tr>

This leads me to guess that you have one soccerteam element containing many team elements. Yes? (It's poor naming, it would be better to call the outer wrapping element "teams").

Change it to:

<xsl:template match="team">
<tr>
    <td><xsl:value-of select="teamid"/></td>
    <td><xsl:value-of select="@name"/></td>
    <td><xsl:value-of select="trainer"/></td>
    <td>...</td>
</tr>

or if you prefer:

<xsl:template match="teams">
  <xsl:for-each select="team">
    <tr>
      <td><xsl:value-of select="teamid"/></td>
      <td><xsl:value-of select="@name"/></td>
      <td><xsl:value-of select="trainer"/></td>
      <td>...</td>
    </tr>

Then the last line becomes

<td><xsl:value-of select="document('soccerleague.xml')//team[teamid = current()/teamid]"/></td>

Also, read about xsl:key, which can improve the speed by indexing.




Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 24th, 2006, 06:55 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok I changed the schema of soccerteam.xml which has the following tree structure
<soccerteam>
<teams name ="">
     <teamid/>
     <trainer/>
</teams>
</soccerteam>
The other one has this structure
<soccerleauge>
<teams>
     <teamid/>
     <league/>
</teams>
</soccerleague>

And as you have described it above I have rewrite the XSL like this
<xsl:template match="/">
    <h2>Team results</h2>
    <table border="1">
        <tr>
            <th>Team Id</th>
            <th>Team Name</th>
            <th>Trainer</th>
            <th>Soccer League</th>
        </tr>
    </table>
</xsl:template>
<xsl:template match="teams">
    <table>
        <xsl:for-each select="teams">
                <tr>
            <td><xsl:value-of select ="teamid"/></td>
            <td><xsl:value-of select="@name"/></td>
            <td><xsl:value-of select="trainer"/></td>
            <td><xsl:value-of select="document('soccerleague.xml')//teams[teamid = current()/teamid]"/></td>
        </tr>
        </xsl:for-each>
    </table>
</xsl:template>
But I cannot see the records now. What am I actually doing wrong? I am really confused right now.

Additional
I have redesigned the XSL like

<xsl:template match="/">
    <h2>Team results</h2>
    <table border="1">
        <tr>
        <th>Team Id</th>
        <th>Team Name</th>
        <th>Trainer</th>
        <th>Soccer League</th>
        </tr>
        <xsl:for-each select="soccerteam/teams">
            <tr>
            <td><xsl:value-of select ="teamid"/></td>
            <td><xsl:value-of select="@name"/></td>
            <td><xsl:value-of select="trainer"/></td>
            <td><xsl:value-of select="document('soccerleague.xml')//teams[teamid = current()/teamid]"/></td>
        </tr>
        </xsl:for-each>
    </table>
</xsl:template>

This time I am seeing the output like

Team Id Team Name Trainer Soccer League
001 Galatasaray Eric Gerets 001 Turkcell Super Lig

Why do I see the Soccerleauge like 001 Turkcell Super Lig? How can I omit 001?


Your attitude determines your altitude
 
Old July 24th, 2006, 08:31 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 clear from your description which of the elements repeats. I usually use a plural name for an element that represents a collection, and a singular name for the elements of that collection. So I would have

<teams>
  <team>...</team>
  <team>...</team>
  <team>...</team>
</teams>

You haven't indicated in your note which of the elements repeats.

But in any case, this is wrong:

<xsl:template match="teams">
    <table>
        <xsl:for-each select="teams">

unless you have a "teams" element whose children are also "teams" elements.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 24th, 2006, 08:42 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok I didn't want to write the whole XML files. Here are the files

soccerteam.xml
<?xml version="1.0" encoding="UTF-8"?>
<soccerteam xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="X:\Private\XML Examples\soccerteam.xsd">
    <teams name="Galatasaray">
        <teamid>001</teamid>
        <trainer>Eric Gerets</trainer>
    </teams>
    <teams name="Bayern München">
        <teamid>002</teamid>
        <trainer>Felix Magath</trainer>
    </teams>
    <teams name="AC Milan">
        <teamid>003</teamid>
        <trainer>Carlo Angelotti</trainer>
    </teams>
</soccerteam>

soccerleague.xml
<?xml version="1.0" encoding="UTF-8"?>
<soccerleauge xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="X:\Private\XML Examples\soccerleague.xsd">
    <teams>
        <teamid>001</teamid>
        <league>Turkcell Super Lig</league>
    </teams>
    <teams>
        <teamid>002</teamid>
        <league>1. Bundesliga</league>
    </teams>
    <teams>
        <teamid>003</teamid>
        <league>Serie A</league>
    </teams>
</soccerleauge>
 So this last <td> line brings the <teamid> and <league> tags according to the teamid which you have described with
  <td><xsl:value-of select="document('soccerleague.xml')//teams[teamid = current()/teamid]"/></td>
But what I wanted to is to call <league> tag according to its id. I tried to match the teamid tags of soccerteam and soccerleague xml files. I am very sorry to disturb you but I have never worked with two XML files together in an XSL file that 's why it is a bit confusing to me.

My latest XSL file is

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/02/xpath-functions" xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes">
  <xsl:template match="/">
    <h2>Team results</h2>
    <table border="1">
        <tr>
        <th>Team Id</th>
        <th>Team Name</th>
        <th>Trainer</th>
        <th>Soccer League</th>
        </tr>
        <xsl:for-each select="soccerteam/teams">
            <tr>
            <td><xsl:value-of select ="teamid"/></td>
            <td><xsl:value-of select="@name"/></td>
            <td><xsl:value-of select="trainer"/></td>
            <td><xsl:value-of select="document('soccerleague.xml')//teams[teamid = current()/teamid]"/></td>
        </tr>
        </xsl:for-each>
    </table>
</xsl:template>
</xsl:stylesheet>
I thank you very much for your help.

Your attitude determines your altitude
 
Old July 24th, 2006, 08:48 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You want

document('soccerleague.xml')//teams[teamid = current()/teamid]/league

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 24th, 2006, 08:52 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks very much. I am relieved. I have written document('soccerleague.xml')//teams/league[teamid = current()/teamid] once instead of document('soccerleague.xml')//teams[teamid = current()/teamid]/league. I can go on then.

Your attitude determines your altitude





Similar Threads
Thread Thread Starter Forum Replies Last Post
Loading XML files in XSLT newbieboobers XSLT 2 March 12th, 2008 11:42 AM
Can 1 xslt transform an xml doc into 2 text files Raju Sarode XSLT 7 November 3rd, 2006 04:10 PM
xslt differencing between two xml files dhollis XSLT 5 August 4th, 2006 08:49 AM
Compare two xml files using xslt sudha XSLT 0 March 10th, 2006 01:04 AM
Merge XML files into a xml file using xslt lxu XML 4 November 6th, 2003 06:01 PM





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