473,616 Members | 2,835 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Selecting the first node of a Sorted Group

Working with a sorted group, the inability to use following-sibling
(which uses Document Order) and convert an RTF (not avaible with the
Parser that we are using) hampered our ability to solve the following
problem. Consider the following set of Data:

<example>
<Credentials time="3">
<UserId>kiss</UserId>
</Credentials>
<Credentials time="1" break="true">
<UserId>bob</UserId>
</Credentials>
<Credentials time="6" break="true">
<UserId>my</UserId>
</Credentials>
<Credentials time="0">
<UserId>Look, </UserId>
</Credentials>
<Credentials time="2">
<UserId>can</UserId>
</Credentials>
<Credentials time="9">
<UserId>tookish </UserId>
</Credentials>
</example>

When sorted in ascending order, the data reads: "Look, bob can kiss my
tookish". For our requirements I am processing it as a descending set:
"tookish my kiss can bob Look,"

That is the first requirement. However, in the even that a node with
the property of "break" equal to "true" is found, processing should
halt (multiple nodes can have the break property, but we really only
care about the first one).
So the output should read: "tookish my"

I devised a solution by using the substring-before operator to select
the first break node's sorted position.

<xsl:param name="breakPos" >
<xsl:for-each select="example/Credentials">
<xsl:sort select="@time" order="descendi ng" />
<xsl:call-template name="countSequ ence">
</xsl:call-template>
</xsl:for-each>
</xsl:param>
<xsl:param name="endElem">
<xsl:value-of select="substri ng-before($breakPo s,'|')" />
</xsl:param>

<xsl:template match="/">
<html>
<body>
<xsl:for-each select="example/Credentials">
<xsl:sort select="@time" order="descendi ng" />
<xsl:if test="($endElem = '') or (position() &lt;= $endElem)">
<xsl:value-of select="UserId" /><br />
</xsl:if>
</xsl:for-each>
</body>
</html>
</xsl:template>

Yeah, this renders the html output:
tookish<br />
my<br />

but I felt like the "endElem" parameter derivation was kind of a hack.
Would there be a better solution- possibly using Meunchian grouping to
perform this fix?

Dec 10 '06 #1
2 2651
Whoops... I forgot the countSequence template. It basically builds the
list of nodes that have break="true" based on their position in the
sorted tree, delimited by the '|' symbol:

<xsl:template name="countSequ ence">
<xsl:choose>
<xsl:when test="(@break = 'true')">
<xsl:value-of select="positio n()" /><xsl:text>|</xsl:text>
</xsl:when>
<xsl:otherwise> </xsl:otherwise>
</xsl:choose>
</xsl:template>

On Dec 9, 9:34 pm, "IcedDante" <fatPunj...@gma il.comwrote:
Working with a sorted group, the inability to use following-sibling
(which uses Document Order) and convert an RTF (not avaible with the
Parser that we are using) hampered our ability to solve the following
problem. Consider the following set of Data:

<example>
<Credentials time="3">
<UserId>kiss</UserId>
</Credentials>
<Credentials time="1" break="true">
<UserId>bob</UserId>
</Credentials>
<Credentials time="6" break="true">
<UserId>my</UserId>
</Credentials>
<Credentials time="0">
<UserId>Look, </UserId>
</Credentials>
<Credentials time="2">
<UserId>can</UserId>
</Credentials>
<Credentials time="9">
<UserId>tookish </UserId>
</Credentials>
</example>

When sorted in ascending order, the data reads: "Look, bob can kiss my
tookish". For our requirements I am processing it as a descending set:
"tookish my kiss can bob Look,"

That is the first requirement. However, in the even that a node with
the property of "break" equal to "true" is found, processing should
halt (multiple nodes can have the break property, but we really only
care about the first one).
So the output should read: "tookish my"

I devised a solution by using the substring-before operator to select
the first break node's sorted position.

<xsl:param name="breakPos" >
<xsl:for-each select="example/Credentials">
<xsl:sort select="@time" order="descendi ng" />
<xsl:call-template name="countSequ ence">
</xsl:call-template>
</xsl:for-each>
</xsl:param>
<xsl:param name="endElem">
<xsl:value-of select="substri ng-before($breakPo s,'|')" />
</xsl:param>

<xsl:template match="/">
<html>
<body>
<xsl:for-each select="example/Credentials">
<xsl:sort select="@time" order="descendi ng" />
<xsl:if test="($endElem = '') or (position() &lt;= $endElem)">
<xsl:value-of select="UserId" /><br />
</xsl:if>
</xsl:for-each>
</body>
</html>
</xsl:template>

Yeah, this renders the html output:
tookish<br />
my<br />

but I felt like the "endElem" parameter derivation was kind of a hack.
Would there be a better solution- possibly using Meunchian grouping to
perform this fix?
Dec 10 '06 #2

IcedDante wrote:
Working with a sorted group, the inability to use
following-sibling (which uses Document Order) and convert
an RTF (not avaible with the Parser that we are using)
Mentioning what processor you're using would've been a good
idea, instead of mentioning just some of its limitations.
hampered our ability to solve the following
problem. Consider the following set of Data:
[XML]
When sorted in ascending order, the data reads: "Look,
bob can kiss my tookish". For our requirements I am
processing it as a descending set: "tookish my kiss can
bob Look,"

That is the first requirement. However, in the even that
a node with the property of "break" equal to "true" is
found, processing should halt (multiple nodes can have
the break property, but we really only care about the
first one). So the output should read: "tookish my"
[partial solution reeking of imperative programming]

It would've been a better idea to post the entire
transformation instead of just parts of it.
Yeah, this renders the html output:
tookish<br />
my<br />
but I felt like the "endElem" parameter derivation was
kind of a hack. Would there be a better solution-
possibly using Meunchian grouping to perform this fix?
I've grown accustomed to XSLT 2.0, so I can't think of any
elegant 1.0 solution off the top of my head. The following
works:

<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<result>
<xsl:apply-templates select="example/Credentials">
<xsl:sort select="@time" order="descendi ng"/>
</xsl:apply-templates>
</result>
</xsl:template>
<xsl:template match="Credenti als"/>
<xsl:template
match=
"
Credentials
[
not
(
../Credentials
[@time>current()/@time][@break='true']
)
]
">
<xsl:value-of select="UserId"/><br/>
</xsl:template>
</xsl:stylesheet>

....but specifying the sorting order in two separate places
in two different formats is a bit ugly, too, of course.

With XSLT 2.0, a much more elegant solution is possible:

<xsl:styleshe et version="2.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node( )" mode="copy">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="sorted">
<xsl:apply-templates
select="example/Credentials" mode="copy">
<xsl:sort select="@time" order="descendi ng"/>
</xsl:apply-templates>
</xsl:variable>
<xsl:apply-templates select="$sorted/Credentials"/>
</xsl:template>
<xsl:template match="Credenti als"/>
<xsl:template
match=
"
Credentials
[
not
(
preceding-sibling::Creden tials[@break='true']
)
]
">
<xsl:value-of select="UserId"/><br/>
</xsl:template>
</xsl:stylesheet>

[Tested with Saxon-8B]

--
Pavel Lepin

Dec 11 '06 #3

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

Similar topics

1
1148
by: Gary | last post by:
Hi Been at this for 2 days now. Each business has several packages which they can sort using sort_order. I'm trying to get one package for each business(that I can do), however I want it to be the one with the lowest sort_order value As you can see below the first record has sort_order=5 when it should be 1. Most of the sort_order columns will be zero by default
0
1088
by: Andreas Håkansson | last post by:
I've been using the Muenchian method to group XML data, however I have run into a situation where I am unsure how to solve it. What I need to do is select distinct groups of items. I have an XML dokument which looks like the one below, where I have 1-n item nodes and each item node can have 2-n detail nodes. The group node can have a value of either one or two, nothing else. <items> <item>
1
1583
by: Nancy Shelley | last post by:
Hi all: I am building a navigation menu using telerik's rad treeview I am able to build the outer menu but not the children. How do I select the child nodes (item) from within the loop? Any help would be greatly appreciated!! Dim node As XmlNode For Each node In xmlDoc.SelectNodes("/configuration/groups/group/menus/menu")
2
2045
by: Mike Kelly | last post by:
Hi. I have a data table where rows are grouped according to a certain criteria and I want to be able to display all the rows that belong to the same group together on the screen. In addition, I want to provide the user with a way of selecting a single group. A radio button is the obvious choice, but other suggestions will be welcome. For example, the following is what I want to build:
2
7491
by: Tymbow | last post by:
I'm building a web application that is analogous to the Windows XP file explorer in function. The left column contains a TreeView, and the right column a DataGrid populated by selecting TreeView nodes. The TreeView populates dynamically as there are a significant number of nodes. The DataGrid displays both the items and the nodes from the TreeView. Using the explorer analogy this means the TreeView shows folders, and the DataGrid folders...
7
1323
by: PatrickRThomas | last post by:
I need help selecting nodes while excluding some of them. Here's an example of XML: <my_xml> <all_items> <item> <key>1</key> <name>Item 1</name> </item> <item>
8
5007
by: John Barleycorn | last post by:
Hi I'm sorry if this question is a bit basic for most people, but I'm really new to VB (I'm using the .NET 2005 Express Edition), but I've set myself a task and would love to see it through. The problem is that an application we use has a config file which looks XML-like and contains the following: <design filerevision="1" fileversion="0">
7
4737
by: Thomas Schmidt | last post by:
Hi all, I need an XPath which selects all nodes of a specific name which start with a text node, i.e. I want the expression to select: <x> abcdefg <y>hijklmn</y> </x>
1
1721
by: DeveloperX | last post by:
Hi, I should probably start with my XML as it makes it easier to explain the problem. <?xml version="1.0" encoding="utf-8" ?> <aa note="top level"> <b1 note="b1"> <c1 note="b1 c1"> <d1 note="b1 c1 d1" />
0
8199
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8145
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
8642
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
8448
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7118
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6097
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
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
4140
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.