473,414 Members | 1,948 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,414 software developers and data experts.

XPath Q: position of given node in arbitrary nodelist

Can this be done with XPath?

I have input XML that is basically of the form:

<list>
<item att="a">s1</item>
<item att="a">s2</item>
<item att="b">s3</item>
<item att="c">s4</item>
<item att="c">s5</item>
</list>

I need to produce output of the form:

1. s1, s2
2. s3
3. s4, s5

Another way of looking at this is to say that I need to know the
position of a given '<item>' node in the list of item nodes whose
attribute 'att' is not the same as that of the preceding node.

(It's a little more complex because I actually need to test two
attributes for non-equality with the attributes of the preceding
sibling, but that's OK).

Is there a way to either (a) get a count of preceding siblings of the
current context node that pass a given test (the test depending on
relative properties of the siblings), or (b) to get the position() of
the current context node in a list of nodes which is _not_ the same as
the current context nodelist.

Advice, even of the form "No, it's not possible, give up", would be very
welcome.

Thanks

Angus

--
"I am here by the will of the people ... and I *will* ["Metrophage",
not leave until I get my raincoat back." Richard Kadrey]
Jul 20 '05 #1
2 4276
This is a grouping problem and a good solution is to use the Muenchian
method for grouping like this:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:key name="kItemAtt" match="item" use="@att"/>

<xsl:template match="/">
<xsl:for-each
select="/*/item[generate-id()
=
generate-id(key('kItemAtt',
@att)[1]
)
]">
<xsl:value-of select="concat(position(), '. ')"/>

<xsl:for-each select="key('kItemAtt', @att)">
<xsl:value-of select="."/>
<xsl:if test="not(position() = last())">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>&#xA;</xsl:text>
</xsl:for-each>

</xsl:template>
</xsl:stylesheet>

When this transformation is applied on your source.xml:

<list>
<item att="a">s1</item>
<item att="a">s2</item>
<item att="b">s3</item>
<item att="c">s4</item>
<item att="c">s5</item>
</list>

the wanted result is produced:

1. s1, s2
2. s3
3. s4, s5
=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
"Angus McIntyre" <an***@pobox.com> wrote in message
news:an*************************@news.verizon.net. ..
Can this be done with XPath?

I have input XML that is basically of the form:

<list>
<item att="a">s1</item>
<item att="a">s2</item>
<item att="b">s3</item>
<item att="c">s4</item>
<item att="c">s5</item>
</list>

I need to produce output of the form:

1. s1, s2
2. s3
3. s4, s5

Another way of looking at this is to say that I need to know the
position of a given '<item>' node in the list of item nodes whose
attribute 'att' is not the same as that of the preceding node.

(It's a little more complex because I actually need to test two
attributes for non-equality with the attributes of the preceding
sibling, but that's OK).

Is there a way to either (a) get a count of preceding siblings of the
current context node that pass a given test (the test depending on
relative properties of the siblings), or (b) to get the position() of
the current context node in a list of nodes which is _not_ the same as
the current context nodelist.

Advice, even of the form "No, it's not possible, give up", would be very
welcome.

Thanks

Angus

--
"I am here by the will of the people ... and I *will* ["Metrophage", not leave until I get my raincoat back." Richard

Kadrey]
Jul 20 '05 #2
In article <bj************@ID-152440.news.uni-berlin.de>,
"Dimitre Novatchev" <dn********@yahoo.com> wrote:
This is a grouping problem and a good solution is to use the Muenchian
method for grouping like this:


Thank you very much for your solution.

I actually found one of my own, which is less elegant, but which I shall
post here anyway just in case it's of use to someone. (Mine generates a
few stray commas, which would be easy enough to fix, but would obfuscate
the code a little).

<xsl:template match="list">
<xsl:apply-templates select="item[position() = 1 or
@att != preceding-sibling::item[1]/@att]"/>
</xsl:template>

<xsl:template match="item">
<xsl:variable name="att"><xsl:value-of select="@att"/></xsl:variable>
<p><xsl:value-of select="position()"/>.
<xsl:value-of select="text()"/>,
<xsl:for-each select="following-sibling::item[@att = $att]">
<xsl:value-of select="text()"/>,
</xsl:for-each>
</p>
</xsl:template>

Thanks again for your help,

Angus

--
"I am here by the will of the people ... and I *will* ["Metrophage",
not leave until I get my raincoat back." Richard Kadrey]
Jul 20 '05 #3

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

Similar topics

0
by: Drake23 | last post by:
Hi, I need your help! I have to appoint the position of the current node in an nodelist. the nodelist I defined in a variable with: <xsl:variable name="CN"...
8
by: Øyvind Jægtnes | last post by:
I'm playing around a bit with XPath and nodelist and i want to extract some info from a RSS feed. The one that i am testing at can be viewed at http://slashdot.org/index.rss Ok.. heres the deal:...
2
by: Chucker | last post by:
Hi folks, I would like to select some nodes from a very large XML document and then apply a stylesheet to the dom fragment / nodelist that is the result of my XPath-Query. I used to use MSXML...
18
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr);...
4
by: SkyHook | last post by:
1. Under the topic "Select Nodes Using XPath Navigation" it says: "All XmlNodeList objects are synchronized with the underlying document, therefore if you ... modify the value of a node, that node...
9
by: DBC User | last post by:
Hi, I have an xml and I am able to use xpath to identify each node that statisfy the selection criteria. I got the node list. I would like to know is it possible to do the following for the...
6
by: Hoss | last post by:
Hello. Because IE and Mozilla have such completely different XML implementations, I have created a class to handle general XML tasks, such as iterating over nodes given an xpath, evaluating an...
1
by: bruce | last post by:
Hi. Got a test web page, that basically has two "<html" tags in it. Examining the page via Firefox/Dom Inspector, I can create a test xpath query "/html/body/form" which gets the target form for...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.