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 9th, 2006, 08:08 AM
Authorized User
 
Join Date: Jun 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default Selecting data using axis in predicate

HI there,

The following is an example of my source xml

<CBAParty>
  <Employment jobType="Cyclists">
    <RelatedKey ID="1"/>
  </Employment>
</CBAParty>
<Party>
  <Employment primaryJob="Yes"/>
    <Key ID="1">
  </Employment>
   <Employment primaryJob="No"/>
    <Key ID="2">
  </Employment>
</Party>

Desired result
<Party>
  <Employment primaryJob="Yes" jobType="Cyclists">
    <Key ID="1">
  </Employment>
   <Employment primaryJob="No">
    <Key ID="2">
  </Employment>
</Party>

This is how I am trying to get the data but it doesnt work

<xsl:copy-of select="/CBAParty/Employment[following-sibling::RelatedKey/@ID=employmentID]/@*"/>

where employmentID = 1 (I created a variable that is assigned when a template matches <Party/Employment>)

This assignment is within the template match = "Party/Employment"
<xsl:variable name="employmentID" select="current()/Key/@ID"/>

Could anyone kindly explain what i am doing wrong? Thks

 
Old July 9th, 2006, 01:02 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You are doing several things wrong.

Firstly, if there is a variable called employmentID then the way to reference it is as $employmentID - note the "$".

Secondly, if the variable is initialized in a different template, then it will be out of scope, so you will not be able to reference it. (More fundamentally, you are assuming a specific order of execution which is not guaranteed)

Thirdly, there is no Employment element in your source XML that has a following-sibling called RelatedKey, so this predicate will always be false.

You can get the result you want as follows (not tested):

<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="CBAParty"/>

<xsl:template match="Party/Employment">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:copy-of select="key('e', Key/@ID)/@jobType"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:key name="e" match="CBAParty/Employment" use="RelatedKey/@ID"/>

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting Data from a table nkrust Javascript 7 January 11th, 2007 05:44 AM
selecting data in datetime intervals Phathu SQL Server 2005 1 August 29th, 2006 03:53 PM
Chapter 6 Selecting Data in Access ,, Help Me zaidqais BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 2 May 5th, 2006 03:48 AM
Selecting Data.. NitinJoshi General .NET 3 January 18th, 2005 02:59 AM
Selecting data from lookup fields hashtlishnii Access 3 September 25th, 2003 11:14 PM





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