Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
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 December 9th, 2010, 06:18 PM
Authorized User
 
Join Date: Dec 2010
Posts: 16
Thanks: 5
Thanked 0 Times in 0 Posts
Default proper use of xsl:key?

Suppose I have the following collection of "word" elements:

Code:
<word>
<id>1</id>
<text>My</text>
</word>
<word>
<id>2</id>
<text>name</text>
</word>
<word>
<id>3</id>
<text>is</text>
</word>
<word>
<id>4</id>
<text>Joe</text>
</word>
Given an integer ID value, in order to find the corresponding TEXT, I can do this:

Code:
<xsl:key name="word-by-id" match="word" use="id"/>
return key('word-by-id', string($ID))/text
But what if word are instead specified as

Code:
<word id=1>
<text>My</text>
</word>
How do I need to change the "xsl:key" in order to retrieve TEXT using ID?

Thanks
 
Old December 9th, 2010, 06:48 PM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Simply change it to use="@id"
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old December 10th, 2010, 11:48 AM
Authorized User
 
Join Date: Dec 2010
Posts: 16
Thanks: 5
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by samjudson View Post
Simply change it to use="@id"
Sam, thanks. One more change -- an error on my part. The word section is actually encapsulated by a "words" element, i.e.

Code:
<words>
   <word id=1>
   ...
   </word>
   ...
</words>
Does this change the key statement or the for statement. Currently when I run the transform I get empty output as if it can't find any matches.

Thanks!
 
Old December 10th, 2010, 12:03 PM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

No, the above should work just fine without any changes to the key function.

If you are not getting anything returned then the error must be in something else youhaven't shown us.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old December 10th, 2010, 12:22 PM
Authorized User
 
Join Date: Dec 2010
Posts: 16
Thanks: 5
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by samjudson View Post
No, the above should work just fine without any changes to the key function.

If you are not getting anything returned then the error must be in something else youhaven't shown us.
OK, let me give you the complete source XML. What I called words before are actually called tokens and sentences are now namedEntities but the idea is still the same.

1. Here's the source XML:
Code:
<?xml version="1.0" encoding="UTF-8"?>

<rexml:document xmlns:rexml='http://www.basistech.com/2003/rexml'>
<header>
  <language>en</language>
<original-encoding>UTF-16</original-encoding>
</header>
<contents>
<tokens>
  <token index='104'>
    <word>WORD_SKIP1</word>
    <position start='572' end='581' />
    <pos>ADJ</pos>
  </token>
  <token index='105'>
    <word>WORD_SKIP2</word>
    <position start='582' end='584' />
    <pos>PREP</pos>
  </token>
  <token index='106'>
    <word>WORD_USE11</word>
    <position start='585' end='591' />
    <pos>NOUN_PROP</pos>
  </token>
  <token index='107'>
    <word>WORD_USE12</word>
    <position start='591' end='592' />
    <pos>PUNC</pos>
  </token>
  <token index='112'>
    <word>WORD_SKIP3</word>
    <position start='604' end='605' />
    <pos>PUNC</pos>
  </token>
  <token index='113'>
    <word>WORD_USE21</word>
    <position start='605' end='610' />
    <pos>NOUN_PROP</pos>
  </token>
  <token index='114'>
    <word>WORD_USE22</word>
    <position start='610' end='611' />
    <pos>PUNC</pos>
  </token>
 </tokens>
<namedEntities>
  <namedEntity>
    <entityStart>106</entityStart>
    <entityEnd>107</entityEnd>
    <entityType>LOCATION</entityType>
  </namedEntity>
  <namedEntity>
    <entityStart>113</entityStart>
    <entityEnd>114</entityEnd>
    <entityType>ORGANIZATION</entityType>
  </namedEntity>
</namedEntities>
</contents>
</rexml:document>
2. Here's my XSLT:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:key name="entity-by-token-index" match="token" use="@index"/>

<xsl:strip-space elements="*"/>

<xsl:template match="/">
	<data>
    <xsl:for-each select="//namedEntities/namedEntity">
		<container>
			<xsl:value-of select="for $index in entityStart to entityEnd-1 
													return key('entity-by-token-index', string($index))/word"/>
		</container>
  </xsl:for-each>
  </data>
</xsl:template>

</xsl:stylesheet>
3. Here's the resulting XML:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<data>
   <container/>
   <container/>
</data>
 
Old December 10th, 2010, 12:39 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

"entityEnd-1" is not an arithmetic expression as you seem to want, rather it selects nodes with the name "entityEnd-1". If you want to substract 1 use a space e.g. "entityEnd - 1" but for the "to" expression you will need
Code:
for $index in entityStart to xs:integer(entityEnd) - 1
to ensure you have integer values. And then you need to put
Code:
xmlns:xs="http://www.w3.org/2001/XMLSchema"
on the xsl: stylesheet element (and probably want exclude-result-prefixes="xs" too).
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
ilyaz (December 10th, 2010)




Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl:key get value of ancestor bonekrusher XSLT 5 April 24th, 2009 11:17 AM
<xsl:key elayaraja.s XSLT 1 July 23rd, 2008 05:00 AM
Using key() and <xsl:key> freddy XSLT 2 January 18th, 2007 08:55 PM
Proper Case IN Xsl Israr XSLT 8 April 4th, 2006 01:39 PM
selection within xsl:key Kabe XSLT 4 February 25th, 2005 05:49 AM





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