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 August 12th, 2011, 01:50 PM
Registered User
 
Join Date: Aug 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation How to refer to the current value in XPath (XSLT)?

I am trying to run this XPath expression (that is, trying to count how many element content strings in my XML file end with letter-one-f ('a') or letter-two-f ('A'):

Code:
<xsl:value-of select="count( substring(.,string-length(.) -1,string-length(.) -1)=$letter-one-f or substring(., string-length(.) -1,string-length(.) -1)=$letter-two-f )"/>
but I don't know how to refer to the 'current value'. All I know is that it's usually represented by a dot '.' . I don't know where to put 'template match' or if that is even needed.

This code, on the other hand, works, because I have specified that it should look in '/n-grams-sorted/n-gram':

Code:
<xsl:value-of select="count(/n-grams-sorted/n-gram[starts-with(.,$letter-one-f) or starts-with(.,$letter-two-f) ])"/>
I just don't know how to apply this to the first expression. Where am I going to get 'the current value'? How am I going to tell it that I want to look in '/n-grams-sorted/n-gram'? It's all I need to get my expression working (I tried it in my editor's 'xpath view').

Source XML file sample:

Code:
<n-grams-sorted analysis="N_GRAM_TOKEN3" range="Total Set">
      <n-gram position="1" frequency="3535" probability="0.0447735">. = =</n-gram>
      <n-gram position="2" frequency="322" probability="0.0040784">= = De</n-gram>
      <n-gram position="3" frequency="284" probability="0.0035971">= = Het</n-gram>
      <n-gram position="4" frequency="207" probability="0.0026218">= = Hij</n-gram>
      <n-gram position="5" frequency="168" probability="0.0021278">= = Dit</n-gram>
I have already tried everything I could think of and this issue has been bothering me for a week. This is what I last tried:

Code:
<xsl:value-of select="count( ./n-gram[substring(current(),1,string-length(current()) -1)=$letter-one-f or substring(current(),1,string-length(current())=$letter-two-f ) ] )"/>

My full code:
http://pastebin.com/TaKB6Bbd

Any ideas?

Thank you,

--
uid123
 
Old August 13th, 2011, 07:23 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

I'm not sure I understood your requirement correctly, but could arrive at the below:

Input XML
Code:
<n-grams-sorted analysis="N_GRAM_TOKEN3" range="Total Set">
      <n-gram position="1" frequency="3535" probability="0.0447735">. = =</n-gram>
      <n-gram position="2" frequency="322" probability="0.0040784">A= = De</n-gram>
      <n-gram position="3" frequency="284" probability="0.0035971">= = Het</n-gram>
      <n-gram position="4" frequency="207" probability="0.0026218">a= = Hij</n-gram>
      <n-gram position="5" frequency="168" probability="0.0021278">= = Dit</n-gram>
   </n-grams-sorted>
XSLT code
Code:
<xsl:template match="//*">
 <xsl:param name="letter-one-f" select=" 'a' "/>
  <xsl:param name="letter-two-f" select=" 'A' "/>
  
 <xsl:value-of select="count(*[starts-with(.,$letter-one-f) or starts-with(.,$letter-two-f) ])"/>
    </xsl:template>
output
2
__________________
Rummy
 
Old August 13th, 2011, 07:42 AM
Registered User
 
Join Date: Aug 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation

Thank you for replying, mrame.

My requirement is: I want to find all the nodes whose content ends with 'a' or 'A'. It's easy to do it for starts-with[], as you showed. However, I am working with Xslt 1.0 and I can't use ends-with[].

So what I am really looking for, is an XSLT 1.0 version of ends-with[].
This can be accomplished by the following code:

Code:
substring(node, start, end)
It can be written as this:

Code:
substring(.,string-length(.) -1, string-length(.) -1)
because . is the current node. string-length(.) takes the string length of this current node, subtracts 1 from it, meaning it begins at the last letter of the content (e.g. substring("sonya", string-length(.) -1, string-length(.) -1) can be translated as: substring("sonya", 5, 5) which would fetch the letter "a".

This is all I need. I need to fetch all nodes that end with 'a' or 'A'.
The problem I am having is that my editor says "is not a node set". So I am guessing that '.' is not a node set.

So all I need is to fetch the 'current node', so I can decide if it ends with an 'a' or 'A'. That's all.
 
Old August 13th, 2011, 08:17 AM
Registered User
 
Join Date: Aug 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Smile

mrame, I have fixed it!!!

Thank you so much!!! =)

Here's the solution:
Code:
<xsl:value-of  select="count(/n-grams-sorted/n-gram[substring(.,string-length(.), string-length(.))=$letter-one-f or substring(.,string-length(.), string-length(.))=$letter-two-f ] )"/>
The whole problem was not the current node.. the problem was that I was getting '0' as a result all the time, because I was doing string-length(.) -1, which is incorrect, since that would mean "sonya" would become "sony" and start at 'y'. 'y' would never result in 'a', that's why 0... so the solution was to use string-length(.) .. without the -1.

I am very happy now! =)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Refer variables from XSLT Include or Import chilly XSLT 1 July 26th, 2010 09:56 AM
XSLT with XPATH [email protected] XSLT 6 December 22nd, 2009 09:40 AM
xslt xpath boris17 XSLT 2 October 15th, 2007 05:31 PM
xpath: position parent of current node orion83 XSLT 1 September 5th, 2005 07:20 AM
XSLT/XPATH Help KombatKarl XSLT 3 September 2nd, 2004 07:35 AM





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