473,796 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4294
This is a grouping problem and a good solution is to use the Muenchian
method for grouping like this:

<xsl:styleshe et 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('kItemAt t',
@att)[1]
)
]">
<xsl:value-of select="concat( position(), '. ')"/>

<xsl:for-each select="key('kI temAtt', @att)">
<xsl:value-of select="."/>
<xsl:if test="not(posit ion() = 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.co m> 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********@yah oo.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="positio n()"/>.
<xsl:value-of select="text()"/>,
<xsl:for-each select="followi ng-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
3111
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" select="self::node()/parent::node()/child::node()" /> now I have to get the position of the current node in $CN can someone help me, please?
8
4241
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: I load xml via httpwebrequest etc and put it in a XmlDocument. xmlFeed.Load(xRes.GetResponseStream()); This works fine as i can use this stream with a XmlReader to parse the
2
3567
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 where I know how to do this. In .NET I am starting to get crazy because I don´t manage to transform my nodelist into an XPath-Navigable-Document. Can Anybody please help me?
18
7739
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); System.out.println(xp.evaluate(new InputSource(new FileInputStream("a.xml"))));
4
4634
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 is updated in the document it came from." 2. Under the topic "XmlNode.SelectNodes Method (String)" it says: "The XmlNodeList should not be expected to be connected "live" to the XML document. That is, changes that appear in the XML diocument...
9
3604
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 following XML <Files> <Application key="one"> <Version>1</Version> <Age>120</Age> </Application>
6
3405
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 xpath, ect. It does all the branching for the different implementations within itself. I am working on a new method for this class that will, given an xpath, remove all nodes that match from the document. It works great in IE, heres the IE code.
1
2218
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 the test. The issue comes when I examine the page's source html. It looks like: <html> <body> </body>
0
9524
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10449
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10168
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6785
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.