473,464 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Preceding with partial string

I seem to have a problem using the preceding:: on substrings of the
value. I'll try to explain with an example:

Let's say I had a XML that look like:

<Root>
<Row>
<Cell Name="Street" Value="2nd Street" />
</Row>
<Row>
<Cell Name="Street" Value="Meadow Lane" />
</Row>
<Row>
<Cell Name="Street" Value="2nd Street" />
</Row>
<Row>
<Cell Name="Street" Value="2nd Street" />
</Row>
<Row>
<Cell Name="Street" Value="Meadow Lane" />
</Row>

</Root>

Let's say I want to find the distinct streets. This is straight
forward and may look something like:
<xsl:template match="Root">
<xsl:element name="root">
<xsl:for-each select="./Row[Cell[@Name = 'Street' and not(@Value =
preceding::Cell[@Name= 'Street']/@Value) ]]">

<xsl:variable name="UniqueStreet" select="./Cell[@Name='Street']/
@Value"/>

<xsl:element name="street">
<xsl:attribute name="name">
<xsl:value-of select="$UniqueStreet"/>
</xsl:attribute>

<!-- This is where I would reiterate through the list to get the
list of residents
that live on that block -->
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>

This returns what I expect:

<root>
<street name="2nd Street"/>
<street name="Meadow Lane"/>
</root>

But let's say the address contained street numbers, and I needed to
strip out the street numbers when trying to look for unique street
names.
<Root>
<Row>
<Cell Name="Address" Value="1345 2nd Street" />
</Row>
<Row>
<Cell Name="Address" Value="332 Meadow Lane" />
</Row>
<Row>
<Cell Name="Address" Value="333 2nd Street" />
</Row>
<Row>
<Cell Name="Address" Value="8534 2nd Street" />
</Row>
<Row>
<Cell Name="Address" Value="556 Meadow Lane" />
</Row>

</Root>

<xsl:template match="Root">
<xsl:element name="root">
<xsl:for-each select="./Row[Cell[@Name = 'Address' and
not(substring-after(@Value, ' ') = substring-
after(preceding::Cell[@Name= 'Address']/@Value, ' ')) ]]">

<xsl:variable name="UniqueStreet" select="substring-after(./
Cell[@Name='Address']/@Value, ' ')"/>

<xsl:element name="street">
<xsl:attribute name="name"><xsl:value-of select="$UniqueStreet"/
></xsl:attribute>
<!-- This is where I would reiterate through the list to get the
list of residents
that live on that block -->
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>

This yields the output:

<root>
<street name="2nd Street"/>
<street name="Meadow Lane"/>
<street name="Meadow Lane"/>
</root>

Can someone explain why that is happening and how I can fix it?
Jun 27 '08 #1
1 2489
rn******@gmail.com wrote:
<xsl:for-each select="./Row[Cell[@Name = 'Street' and not(@Value =
preceding::Cell[@Name= 'Street']/@Value) ]]">
<xsl:for-each select="./Row[Cell[@Name = 'Address' and
not(substring-after(@Value, ' ') = substring-
after(preceding::Cell[@Name= 'Address']/@Value, ' ')) ]]">
Can someone explain why that is happening and how I can fix it?
The first comparison compares two node-sets (one with the single
attribute @Value, one with all @Value attributes of preceding::Cell
elements).
The second comparison however compares the single @Value attribute to
the string result of substring-after applied to the node-set of @Value
attributes of preceding::Cell elements and that way you only compare to
one preceding attribute, not to all.

With XSLT 2.0 you could use

<xsl:for-each select="./Row[Cell[@Name = 'Address' and
not(substring-after(@Value, ' ') = preceding::Cell[@Name=
'Address']/@Value/substring-after(., ' ')) ]]">

to make sure you compare to a sequence and not a string value but that
solution does not exist in XSLT 1.0.

And XSLT 2.0 has other (easier) ways to identify distinct values, there
is the function distinct-values
http://www.w3.org/TR/xpath-functions...istinct-values and there is
xsl:for-each-group.
With XSLT 1.0 you need to use Muenchian grouping:

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

<xsl:key name="by-street"
match="Row"
use="substring-after(Cell[@Name = 'Address']/@Value, ' ')"/>

<xsl:template match="Root">
<root>
<xsl:for-each
select="Row[generate-id() = generate-id(key('by-street',
substring-after(Cell[@Name = 'Address']/@Value, ' '))[1])]">
<street>
<xsl:value-of select="substring-after(Cell[@Name =
'Address']/@Value, ' ')"/>
</street>
</xsl:for-each>
</root>
</xsl:template>

</xsl:stylesheet>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 27 '08 #2

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

Similar topics

2
by: Sven | last post by:
Hi I have the following XML & XSLT: XML: <?xml version="1.0" encoding="utf-8"?> <Source Source="IBM"> <Detail> <UserID></UserID>
16
by: pawel.pabich | last post by:
Hajo, I would like to have 2 my own partial classes. For example: Default.aspx.cs Default2.aspx.cs and they both will relate to Default.aspx page.
10
by: ptass | last post by:
Hi In asp.net 2.0 an aspx files .cs file is a partial class and all works fine, however, I thought I’d be able to create another class file, call it a partial class and have that compile and...
1
by: chungiemo | last post by:
Hi thought I would do another thread as this one is a bit different from the previous problem I am looking for a solution to the relating problem Comparing 2 access databases with 2 tables,...
1
by: hardieca | last post by:
Hi! I decorate my unfinished classes and methods with a custom TODO attribute (as in things To Do). Using reflection, I am then able to parse through my classes and output all TODOs to a list I...
2
by: saengpole | last post by:
Hi all, I'm a newbie in VB2005. I have a trouble getting text from textbox and combobox.SelectedValue. The values reside on two controls are text. But they are in numeric text eg. product code...
4
by: Phil Barber | last post by:
I have two web pages in a VS 2005 project. the first page has a struct defined and used in the partial class: public partial class InterIndex : System.Web.UI.Page { public struct TXMLData {...
5
by: SAL | last post by:
Hello, I would like to be able to set the WHERE clause of a select statement on the fly. I have a DataAccess layer designed using the DataSet designer and a BusinessLogic layer using classes. The...
10
by: JDeats | last post by:
So I have a class that spans over two partial classes in code, here's an example (do not read much into this, the code is of no practical use, this is just a simple example of where my confusion...
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
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...
1
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...
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...
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...
0
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...

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.