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 January 22nd, 2010, 05:37 AM
Authorized User
 
Join Date: Jan 2010
Posts: 33
Thanks: 13
Thanked 0 Times in 0 Posts
Default How to set one of the attributes as id key

Hi,

Can someone please give me a hand with setting CustomerId as an Id Key. It would help me if I can select all data in xmldatasource with this Id Key equal to some index. Below is my current xsl file
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>

<xsl:template match="Employees">
<Employees>
<xsl:apply-templates select="Employee"/>
</Employees>
</xsl:template>
<xsl:template match="Employee">
<Employee>
<xsl:attribute name="CustomerId">
<xsl:value-of select="CustomerId"/>
</xsl:attribute>
<xsl:attribute name="FirstName">
<xsl:value-of select="FirstName"/>
</xsl:attribute>
<xsl:attribute name="LastName">
<xsl:value-of select="LastName"/>
</xsl:attribute>
</Employee>
</xsl:template>
</xsl:stylesheet>


Thanks,
 
Old January 22nd, 2010, 05:55 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Declare

<xsl:key name="customerId-key" match="Employee" use="CustomerId"/>

as a child of xsl:stylesheet. Then you can locate Employee "12345" using

key('customerId-key', '12345')

It seems a bit odd for employees to have a customer number, but the world moves in strange ways....
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
jtnchang (January 22nd, 2010)
 
Old January 22nd, 2010, 06:14 AM
Authorized User
 
Join Date: Jan 2010
Posts: 33
Thanks: 13
Thanked 0 Times in 0 Posts
Default

Thanks for the prompt reply.
 
Old January 22nd, 2010, 10:11 PM
Authorized User
 
Join Date: Jan 2010
Posts: 33
Thanks: 13
Thanked 0 Times in 0 Posts
Default

Michael,

That was my test xsl file. The real one looks like below after you gave me info in another thread. What do I need to change in order for me to retrieve data with key set to a value?

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
hotelId
<xsl:key name="hotelId-key" match="Hotel" use="hotelId"/>
<xsl:template match="HotelAvailabilityListResults">
<HotelAvailabilityListResults>
<xsl:apply-templates select="Hotel"/>
</HotelAvailabilityListResults>
</xsl:template>

<xsl:template match="Hotel">
<xsl:copy>
<xsl:copy-of select="name | address1 | city | stateProvince | country | postalCode | shortDescription | HotelProperty/RateInfo"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Thanks,
 
Old January 23rd, 2010, 10:40 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can retrieve a hotel using

key('hotelId-key', 'id123456789')
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
wild & id key comparison and add - discrepancy fin JohnSeito Excel VBA 0 November 27th, 2007 11:32 PM
how to parse xml with an attributes of type ID kirkpng XSLT 1 January 25th, 2005 04:44 PM
How to Auto Generate ID (Primary Key) SQL Database havering SQL Server ASP 1 December 9th, 2004 05:33 AM
How to Auto Generate ID (Primary Key) SQL database havering SQL Server 2000 9 December 1st, 2004 10:38 AM
Defining ID attributes Fiddleman XML 1 June 30th, 2004 09:41 AM





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