473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Q: (XSLT) finding the position of the first occurrence of an attribute value

hello everyone.

i have the sort of question that makes me feel like i am missing the forest
for the trees, so apologies if i am missing the blatantly obvious here. i am
using saxon, and mostly xslt version 1.1 (the unofficial one).

i have a template parameter, say v, which contains a sequence of nodes of
the same kind, say A, which all have an attribute, say n. these attributes
may have different numeric values. i have identified the least of those like
this:
<xsl:variable name="minN" select="min($v/@n)"/>

what i'd like to do now is to compute the index (in v) of the *first* node
whose n attribute has value $minN. iow, i'd like to write something like
<xsl:variable name="pos" select="???"/>

where ??? would be an xpath-expression computing the desired position. so
e.g. if v contained the nodes
<A n="5"/><A n="3"/><A n="4"/><A n="5"/><A n="3"/>
then minN would be 3, and pos should be 2. i have tried a few things that
seemed to produce correct results if there was only one node with the
corresponding attribute value, but i was not successful at finding a
solution that works in cases like the one above.

any input is greatly appreciated. thanks for your time.

regards,

-- j


Jul 20 '05 #1
3 4935

"Jorn W Janneck" <jw*******@comcast.net> wrote in message
news:gGh5b.342740$uu5.68257@sccrnsc04...
hello everyone.

i have the sort of question that makes me feel like i am missing the forest for the trees, so apologies if i am missing the blatantly obvious here. i am using saxon, and mostly xslt version 1.1 (the unofficial one).

i have a template parameter, say v, which contains a sequence of nodes of
the same kind, say A, which all have an attribute, say n. these attributes
may have different numeric values. i have identified the least of those like this:
<xsl:variable name="minN" select="min($v/@n)"/>
There is no function min() in XPath 1.0
what i'd like to do now is to compute the index (in v) of the *first* node
whose n attribute has value $minN. iow, i'd like to write something like
<xsl:variable name="pos" select="???"/>


In the general case this is:

count(set:leading($v[@n = $minN][1])) + 1

where set:leading is the identically named extention function from EXSLT.

In case if all nodes in $v belong to the same document, then one can use
this XPath expression:

count(
$v[count(. | $v[@n = $minN][1]/preceding::*) = count($v[@n =
$minN][1]/preceding::*)
or
count(. | $v[@n = $minN][1]/ancestor::*) = count($v[@n =
$minN][1]/ancestor::*)
]
) + 1

Or you may first produce from $v an RTF, convert it to a tree $v1 and then
use just:

count($v1/*[@n = $minN][1]/preceding-sibling::*) + 1

=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

Jul 20 '05 #2
Also, for an efficient XSLT implementation of set:leading() see my
article in xml.com "EXSLT for MSXML" at:

http://www.xml.com/pub/a/2003/08/06/exslt.html
=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

"Dimitre Novatchev" <dn********@yahoo.com> wrote in message news:<bj************@ID-152440.news.uni-berlin.de>...
"Jorn W Janneck" <jw*******@comcast.net> wrote in message
news:gGh5b.342740$uu5.68257@sccrnsc04...
hello everyone.

i have the sort of question that makes me feel like i am missing the

forest
for the trees, so apologies if i am missing the blatantly obvious here. i

am
using saxon, and mostly xslt version 1.1 (the unofficial one).

i have a template parameter, say v, which contains a sequence of nodes of
the same kind, say A, which all have an attribute, say n. these attributes
may have different numeric values. i have identified the least of those

like
this:
<xsl:variable name="minN" select="min($v/@n)"/>


There is no function min() in XPath 1.0

what i'd like to do now is to compute the index (in v) of the *first* node
whose n attribute has value $minN. iow, i'd like to write something like
<xsl:variable name="pos" select="???"/>


In the general case this is:

count(set:leading($v[@n = $minN][1])) + 1

where set:leading is the identically named extention function from EXSLT.

In case if all nodes in $v belong to the same document, then one can use
this XPath expression:

count(
$v[count(. | $v[@n = $minN][1]/preceding::*) = count($v[@n =
$minN][1]/preceding::*)
or
count(. | $v[@n = $minN][1]/ancestor::*) = count($v[@n =
$minN][1]/ancestor::*)
]
) + 1

Or you may first produce from $v an RTF, convert it to a tree $v1 and then
use just:

count($v1/*[@n = $minN][1]/preceding-sibling::*) + 1

=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

Jul 20 '05 #3
Dimitre:

Thanks so much for your extensive and knowledgeable answer. Not only did you
solve my problem, it was also quite instructive. I really appreciate the
effort you put into it. I had stared at that problem for quite a while.

[snip several suggestions]
Or you may first produce from $v an RTF, convert it to a tree $v1 and then
use just:

count($v1/*[@n = $minN][1]/preceding-sibling::*) + 1


This is what I ended up doing. Does the job beautifully.

Thanks again,

-- j
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Iain | last post by:
I want to create an XML configuration file which might look like <REGION Name="Europe" WingDing="Blue"> <COUNTRY Name="UK" WingDing="white"> <TOWN Name="London" WingDing="Orange" /> </COUNTRY>...
1
by: Oleg Konovalov | last post by:
Hi, I am new to XSLT, modifying somebody else's code: I have the following data (leaves in parallel branches): mystruct/myarray1/myvar/var2 and mystruct/myarray2/myvar/var3 I need to...
2
by: TadPole | last post by:
Hi all, My main problems are::::::::: 1. Set a value within a block container that can be used and changed by subsequent templates/block-containers/tables etc.. 2. get/determine/find the...
6
by: John Bailo | last post by:
Given this XML: <?xml version="1.0" encoding="UTF-8"?> <pallet> <position row="0" bay="0" level="A"> <client id="ABC"></client> </position> <position row="1" bay="1" level="B"> <client...
2
by: Ch Pravin | last post by:
Hi All: I am having the following xml which i need to convert to excel using xslt. Please help me out. Afghanistan.xml <?xml version="1.0" encoding="utf-16"?> <Languages...
2
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values...
3
by: z1 | last post by:
hi- i am fooling around with soap and weather templates. for some reason either this if or select is failing. i am very new to xml and found this code at another site. i can show you the xml...
8
by: manugm1987 | last post by:
<?xml-stylesheet type="text/xsl" href="ch1.xsl"?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <style type="text/css"> span.cls_002{...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.