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'):

<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':

<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:

<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:

<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,

--
uid1

I apologize if I have not been so clear on my requirements.

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[]. 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, 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.

Thank you

I have fixed it!!!

Here's the solution:

<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! =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.