xsl-list
[Top] [All Lists]

RE: [xsl] Use of xsl:key to perform node traversal according to an association between nodes

2007-09-26 09:23:31
It works fine now. 

Amending the XSLT as you suggested to become:

<?xml version='1.0'?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml"/>

<xsl:key name="player-id" match="Teams_and_Players/Players/Player"
use="@id"/>

<xsl:template match="/">
  <xsl:message> root found </xsl:message>
  <xsl:apply-templates select="Teams_and_Players"/>
</xsl:template>

<xsl:template match="Teams_and_Players">
 <Teams>
  <xsl:message> Teams_and_Players found </xsl:message>
  <xsl:apply-templates select="Teams"/>
 </Teams>       
</xsl:template>

<xsl:template match="Teams">
  <xsl:message> Teams found </xsl:message>
  <xsl:apply-templates select="Team"/>
</xsl:template>

<xsl:template match="Team">
  <Team>
  <xsl:message> Team found </xsl:message>
  <xsl:value-of select="@name"/>
  <xsl:apply-templates select="TeamPlayer"/>
  </Team>
</xsl:template>

<xsl:template match="TeamPlayer">
  <xsl:message> TeamPlayer found </xsl:message>
  <Player>
  <xsl:value-of select="key('player-id', .)/@name"/>
  </Player>
</xsl:template>
</xsl:stylesheet>

Once again, many thanks to Wendell Piez, Scott Trenda and Brad
Bjorndahl.

Best regards,

Mike


-----Original Message-----
From: Wendell Piez [mailto:wapiez(_at_)mulberrytech(_dot_)com] 
Sent: 26 September 2007 16:34
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] Use of xsl:key to perform node traversal according to
an association between nodes

Mike,

At 10:29 AM 9/26/2007, Brad wrote:
I think it's correct except for:
<xsl:value-of select="key('player-id', @id)"/>

which should be changed to:
<xsl:value-of select="key('player-id', .)"/>

because it's the content for TeamPlayer that you need to match to 
the key entries. TeamPlayer does not have an id attribute.

Also, once you've correctly selected the Player node, you presumably 
want to output its @name, not its value (since it's empty).

So that would be

<xsl:value-of select="key('player-id', .)/@name"/>

Or there might be times (with problems of real-world complexity) when 
you even want to do:

<xsl:apply-templates select="key('player-id', .)" mode="name"/>

<xsl:template match="Player" mode="name">
   <xsl:value-of select="@name"/>
</xsl:template>

Cheers,
Wendell

-----Original Message-----
From: Dunk, Michael (Mike) [mailto:mikedunk(_at_)fairisaac(_dot_)com]
Sent: September 26, 2007 10:15 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Use of xsl:key to perform node traversal according to 
an association between nodes

...

 Here is an simplified example of the type of xml input I need to
read:

<?xml version="1.0" encoding="UTF-8"?>
<Teams_and_Players>
  <Teams>
    <Team name="Man Utd">

<TeamPlayer>{0EA17FA0-D21B-4F1E-8B11-A250A6CF266C}+00000000</TeamPlayer


<TeamPlayer>{1F3E974C-F180-45EB-AB59-4D1ACF4BB855}+00000000</TeamPlayer


<TeamPlayer>{3F9BEEE0-35DB-4835-A89E-F2B240EC2CB0}+00000000</TeamPlayer

    </Team>
  </Teams>
  <Players>
    <Player id="{0EA17FA0-D21B-4F1E-8B11-A250A6CF266C}+00000000"
name="Gary Neville"></Player>
    <Player id="{1F3E974C-F180-45EB-AB59-4D1ACF4BB855}+00000000"
name="Louis Saha"></Player>
    <Player id="{3F9BEEE0-35DB-4835-A89E-F2B240EC2CB0}+00000000"
name="Ryan Giggs"></Player>
  </Players>
</Teams_and_Players>


The xslt written to list the teams and players is :

<?xml version='1.0'?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml"/>

<xsl:key name="player-id" match="Teams_and_Players/Players/Player"
use="@id"/>

<xsl:template match="/">
  <xsl:apply-templates select="Teams_and_Players"/> </xsl:template>

<xsl:template match="Teams_and_Players">  <Teams>
  <xsl:apply-templates select="Teams"/>
 </Teams>
</xsl:template>

<xsl:template match="Teams">
  <xsl:apply-templates select="Team"/>
</xsl:template>

<xsl:template match="Team">
  <Team>
  <xsl:value-of select="@name"/>
  <xsl:apply-templates select="TeamPlayer"/>
  </Team>
</xsl:template>

<xsl:template match="TeamPlayer">
  <Player>
  <xsl:value-of select="key('player-id', @id)"/>
  </Player>
</xsl:template>
</xsl:stylesheet>

Currently the following is output:

<?xml version="1.0" encoding="UTF-8"?>
<Teams>
  <Team>Man Utd
    <Player/>
    <Player/>
    <Player/>
  </Team>
</Teams>

Has anyone got any clues about what is wrong with?

<xsl:key name="player-id" match="Teams_and_Players/Players/Player"
use="@id"/>

or

<xsl:value-of select="key('player-id', @id)"/>

Best regards,

Mike


This email and any files transmitted with it are confidential, 
proprietary and intended solely for the individual or entity to whom 
they are addressed..
If you have received this email in error please delete it immediately.



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


======================================================================
Wendell Piez                            
mailto:wapiez(_at_)mulberrytech(_dot_)com
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
   Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

This email and any files transmitted with it are confidential, proprietary
and intended solely for the individual or entity to whom they are addressed.
If you have received this email in error please delete it immediately.


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>
  • RE: [xsl] Use of xsl:key to perform node traversal according to an association between nodes, Dunk, Michael (Mike) <=