473,662 Members | 2,622 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

This XSLT problem makes no sense to me

Context:

I'm trying to compare XML tree fragments and I'm doing so by
outputting the attributes of each element in the tree and outputting
it to a string then normalizing the strings. Then I'm doing a contains
of the current string against the following-sibling::* to determine if
we have duplicates. If we have a duplicate, we move to the next item,
if there is no duplicate, we output the small tree.

I'm hitting a completely ridiculous problem that I can't wrap my head
around. I spend easily 2 hours trying to understand why this happened
yesterday and I'm about to punch the computer. When I output a string
that's not directly tied to the logic (or at least apparently not tied
to the logic), the logic works. When I remove the string output, the
logic stops working. Argh!

Here's the XSLT snippet

<xsl:template match="item-wrapper" mode="string">

<xsl:variable name="current">
<xsl:apply-templates mode="string" />
</xsl:variable>

<xsl:variable name="rest">
<xsl:apply-templates select="followi ng-sibling::*"
mode="string" />
</xsl:variable>

<xsl:variable name="current-normalized">
<xsl:value-of select="normali ze-space($current) " />
</xsl:variable>

<xsl:variable name="rest-normalized">
<xsl:value-of select="normali ze-space($rest)" />
</xsl:variable>

<!-- <fix/What the hell? -->
<!-- <xsl:value-of select="$curren t-normalized"/>-->

<xsl:choose>
<xsl:when test="contains( $rest-normalized, $current-
normalized)">
<duplicate/>
</xsl:when>
<xsl:otherwis e>
<noproblem/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

When <xsl:value-of select="$curren t-normalized"/is not commented
out, I get the string representation of the XML tree in a normalized
form in the output for each item and the correct <duplicate/and
<noproblem/get outputted in the correct location after the
normalized strings. When I comment out the <xsl:value-of
select="$curren t-normalized"/>, the normalized current string, as
expected, stops being outputted, but all my outputs are <noproblem/>.
What the hell? What am I missing here?

Regards
Jean-Francois Michaud
Mar 11 '08 #1
11 2076
Hard to be certain without seeing a full runnable copy, but this sounds
more like a bug in your XSLT processor than an expected behavior of XSLT.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Mar 11 '08 #2
On Mar 11, 10:24 am, Joseph Kesselman <keshlam-nos...@comcast. net>
wrote:
Hard to be certain without seeing a full runnable copy, but this sounds
more like a bug in your XSLT processor than an expected behavior of XSLT.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Thanks much. I thought I was going insane ;-). I tried with SaxonB9
and I get the same behavior. I'd have to try with Xalan, but I'd have
to modify the code a bit because Xalan doesn't behave exactly the same
way Saxon8/B9 does.

Regards
Jean-Francois Michaud
Mar 11 '08 #3
Jean-François Michaud wrote:
Context:

I'm trying to compare XML tree fragments and I'm doing so by
outputting the attributes of each element in the tree and outputting
it to a string then normalizing the strings. Then I'm doing a contains
of the current string against the following-sibling::* to determine if
we have duplicates. If we have a duplicate, we move to the next item,
if there is no duplicate, we output the small tree.

I'm hitting a completely ridiculous problem that I can't wrap my head
around. I spend easily 2 hours trying to understand why this happened
yesterday and I'm about to punch the computer. When I output a string
that's not directly tied to the logic (or at least apparently not tied
to the logic), the logic works. When I remove the string output, the
logic stops working. Argh!

Here's the XSLT snippet

<xsl:template match="item-wrapper" mode="string">

<xsl:variable name="current">
<xsl:apply-templates mode="string" />
</xsl:variable>

<xsl:variable name="rest">
<xsl:apply-templates select="followi ng-sibling::*"
mode="string" />
</xsl:variable>

<xsl:variable name="current-normalized">
<xsl:value-of select="normali ze-space($current) " />
</xsl:variable>

<xsl:variable name="rest-normalized">
<xsl:value-of select="normali ze-space($rest)" />
</xsl:variable>

<!-- <fix/What the hell? -->
<!-- <xsl:value-of select="$curren t-normalized"/>-->

<xsl:choose>
<xsl:when test="contains( $rest-normalized, $current-
normalized)">
<duplicate/>
</xsl:when>
<xsl:otherwis e>
<noproblem/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

When <xsl:value-of select="$curren t-normalized"/is not commented
out, I get the string representation of the XML tree in a normalized
form in the output for each item and the correct <duplicate/and
<noproblem/get outputted in the correct location after the
normalized strings. When I comment out the <xsl:value-of
select="$curren t-normalized"/>, the normalized current string, as
expected, stops being outputted, but all my outputs are <noproblem/>.
What the hell? What am I missing here?

Regards
Jean-Francois Michaud
without seeing full input and code it's hard to really tell, but the
behaviour seems expected to me.
with the code as it is (with the value-of commented out) the template
never puts any text into the result tree, just empty elements
<duplicate/or <noproblem/so if this template is typical, then
<xsl:variable name="rest">
<xsl:apply-templates select="followi ng-sibling::*" mode="string"/>
</xsl:variable>

will just produce a sequence of empty elements, so the string value will
be "" so

<xsl:when test="contains( $rest-normalized, $current-normalized)">

is testing if "" contains as a substring the normalised string value of
the current node, which will presumably test as false so you get
<noproblem/every time.

As you are using saxon9 (thus xslt2) you probably want to be looking as
xsl:for-each-group to do duplicate removal, which is likely to be much
more efficient.

David

--
http://dpcarlisle.blogspot.com
Mar 11 '08 #4
In article <f7************ *************** *******@e23g200 0prf.googlegrou ps.com>,
Jean-François Michaud <co*****@comcas t.netwrote:
>When I output a string
that's not directly tied to the logic (or at least apparently not tied
to the logic), the logic works. When I remove the string output, the
logic stops working. Argh!
Look carefully:
<xsl:template match="item-wrapper" mode="string">

<xsl:variable name="current">
<xsl:apply-templates mode="string" />
</xsl:variable>
$current is set to the result of applying the string-mode templates to
the children of the current node. And this *is* the string-mode template.
So when you comment out this:
><!-- <xsl:value-of select="$curren t-normalized"/>-->
you are changing $current!

-- Richard
--
:wq
Mar 11 '08 #5
On Mar 11, 3:01 pm, David Carlisle <david-n...@dcarlisle. demon.co.uk>
wrote:
Jean-François Michaud wrote:
Context:
I'm trying to compare XML tree fragments and I'm doing so by
outputting the attributes of each element in the tree and outputting
it to a string then normalizing the strings. Then I'm doing a contains
of the current string against the following-sibling::* to determine if
we have duplicates. If we have a duplicate, we move to the next item,
if there is no duplicate, we output the small tree.
I'm hitting a completely ridiculous problem that I can't wrap my head
around. I spend easily 2 hours trying to understand why this happened
yesterday and I'm about to punch the computer. When I output a string
that's not directly tied to the logic (or at least apparently not tied
to the logic), the logic works. When I remove the string output, the
logic stops working. Argh!
Here's the XSLT snippet
<xsl:template match="item-wrapper" mode="string">
<xsl:variable name="current">
<xsl:apply-templates mode="string" />
</xsl:variable>
<xsl:variable name="rest">
<xsl:apply-templates select="followi ng-sibling::*"
mode="string" />
</xsl:variable>
<xsl:variable name="current-normalized">
<xsl:value-of select="normali ze-space($current) " />
</xsl:variable>
<xsl:variable name="rest-normalized">
<xsl:value-of select="normali ze-space($rest)" />
</xsl:variable>
<!-- <fix/What the hell? -->
<!-- <xsl:value-of select="$curren t-normalized"/>-->
<xsl:choose>
<xsl:when test="contains( $rest-normalized, $current-
normalized)">
<duplicate/>
</xsl:when>
<xsl:otherwis e>
<noproblem/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
When <xsl:value-of select="$curren t-normalized"/is not commented
out, I get the string representation of the XML tree in a normalized
form in the output for each item and the correct <duplicate/and
<noproblem/get outputted in the correct location after the
normalized strings. When I comment out the <xsl:value-of
select="$curren t-normalized"/>, the normalized current string, as
expected, stops being outputted, but all my outputs are <noproblem/>.
What the hell? What am I missing here?
Regards
Jean-Francois Michaud

without seeing full input and code it's hard to really tell
Understandably but I can't really produce a subset at this time, it
would be too time consuming so I opted for the problem area.

, but the
behaviour seems expected to me.
Hmmm.
with the code as it is (with the value-of commented out) the template
never puts any text into the result tree, just empty elements
<duplicate/or <noproblem/>
That's what I had in mind and is the behavior that I expected to test
out the logic and see if duplicates vs non-duplicates were recognized
correctly.

so if this template is typical, then
<xsl:variable name="rest">
<xsl:apply-templates select="followi ng-sibling::*" mode="string"/>
</xsl:variable>

will just produce a sequence of empty elements, so the string value will
be "" so
Nope! The string templates actually take the attribute names and
values of each element found and outputs it's content so we end up
with a long string of concatenated attribute names and attribute
values. The string is used to effectively "condense" an XML tree into
a string form for comparison purposes. The idea is to test and verify
that two XML trees are identical or not.

The rest variable then contains a similar soup of long string of
attribute names and values (of ALL the siblings). The content is then
normalized to avoid potential whitespace inconsistencies (same with
the current node) and then I simply verify that the current node in a
string form isn't found anywhere in the following siblings string
form, effectively determining that the current node and all it's
children isn't found in any of it's siblings.
<xsl:when test="contains( $rest-normalized, $current-normalized)">

is testing if "" contains as a substring the normalized string value of
the current node, which will presumably test as false so you get
<noproblem/every time.
This seems to be a reasonable explanation for <noproblembei ng
outputted everywhere.

The magic bit though is that when you uncomment out the <xsl:value-of
select="$curren t-normalized">, which, as I understand, has nothing to
do with the evaluation of $rest against the following siblings in a
string form or $rest-normalized or the logic layed down to output
<noproblem/or <duplicate/>, the logic actually outputs <noproblem>
where I expect and <duplicateswher e I expect ;-).

If you're explanation was right, $rest being empty and by extension,
$rest-normalized being empty, it would imply that outputting value-of
$current-normalized somehow made $rest get populated correctly. That
is not what I would call expectable behavior. Or maybe I'm missing
something else?
As you are using saxon9 (thus xslt2) you probably want to be looking as
xsl:for-each-group to do duplicate removal, which is likely to be much
more efficient.
Actually using Saxon8 and we dont' really have the option of hopping
to SaxonB9 at this point in time ;-).

Regards
Jean-Francois Michaud
Mar 12 '08 #6
Jean-François Michaud wrote:
Understandably but I can't really produce a subset at this time, it
would be too time consuming so I opted for the problem area.
Unfortunately, without adequate context it takes a lot of *our* type to
try to diagnose what's going on. If it isn't worth your time, is it
worth ours?

For example: You're producing current by recursion in string mode. The
actual change may be happening when an inner item-wrapper is processed;
it now contains something that wasn't expected and may be changing
behavior in another template you haven't shown us, thus changing
current's value.

Without seeing the rest of the stylesheet, we can't do more than guess
at that kind of interaction.

Spend the time to reduce your problem to a runnable minimal testcase. In
the course of doing so you may solve it yourself. If not, you'll at
least have something other people can help you with.
--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Mar 12 '08 #7
On Mar 11, 3:52 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
In article <f7b1c26b-f0c3-466b-987a-a28e77521...@e2 3g2000prf.googl egroups.com>,
Jean-François Michaud <come...@comcas t.netwrote:
When I output a string
that's not directly tied to the logic (or at least apparently not tied
to the logic), the logic works. When I remove the string output, the
logic stops working. Argh!

Look carefully:
<xsl:template match="item-wrapper" mode="string">
<xsl:variable name="current">
<xsl:apply-templates mode="string" />
</xsl:variable>

$current is set to the result of applying the string-mode templates to
the children of the current node. And this *is* the string-mode template.
So when you comment out this:
<!-- <xsl:value-of select="$curren t-normalized"/>-->

you are changing $current!
An interesting idea, but since the whole stylesheet isn't around, from
my understanding, this is not the case (this is an interresting twist
through and something might be happening there that I'm not perceiving
correctly). item-wrapper contains other elements and templates exist
(mode="string") for those elements also. As mentioned in the answer to
David Carlisle, those templates only output the name and value of each
of their attributes, effectively creating a long string that uniquely
identifies (hopefully) the XML tree under item-wrapper.

Also, because of the hierarchical relationship of nodes, I would
expect $current, to be isolated from the $current of other calls. If
it wasn't, we'd certainly be in trouble ;-).

Regards
Jean-Francois Michaud
Mar 12 '08 #8
On Mar 12, 10:43 am, Joseph Kesselman <keshlam-nos...@comcast. net>
wrote:
Jean-François Michaud wrote:
Understandably but I can't really produce a subset at this time, it
would be too time consuming so I opted for the problem area.

Unfortunately, without adequate context it takes a lot of *our* type to
try to diagnose what's going on. If it isn't worth your time, is it
worth ours?
I understand, I just thought I'd get some thoughts flowing that could
help me pinpoint the problem. I'll spend some time and try and extract
a subset. This would further my understanding of XSLT in any case If
there is something wrong with my code or would help us pinpoint the
Saxon bug if this is the real underlying problem.

I implemented another solution from scratch through string reduction
prior to right hand XML tree creation and hit a silly node-set/for-
each scope limitation. I had to settle for a less graceful solution
(variant on the node-set/for-each solution) that seems to now be
working. I still want to understand what happened on this one though.
For example: You're producing current by recursion in string mode. The
actual change may be happening when an inner item-wrapper is processed;
it now contains something that wasn't expected and may be changing
behavior in another template you haven't shown us, thus changing
current's value.

Without seeing the rest of the stylesheet, we can't do more than guess
at that kind of interaction.

Spend the time to reduce your problem to a runnable minimal testcase. In
the course of doing so you may solve it yourself. If not, you'll at
least have something other people can help you with.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Regards
Jean-Francois Michaud
Mar 12 '08 #9
Jean-François Michaud wrote:

>
>with the code as it is (with the value-of commented out) the template
never puts any text into the result tree, just empty elements
<duplicate/or <noproblem/>

That's what I had in mind and is the behavior that I expected to test
out the logic and see if duplicates vs non-duplicates were recognized
correctly.

so if this template is typical, then
> <xsl:variable name="rest">
<xsl:apply-templates select="followi ng-sibling::*" mode="string"/>
</xsl:variable>

will just produce a sequence of empty elements, so the string value will
be "" so

Nope! The string templates actually take the attribute names and
values of each element found and outputs it's content
The teplate you posted outputs nothing, which is teh cause of your
problem. When you remove the comment then it outputs something, and your
problem goes.

so we end up
with a long string of concatenated attribute names and attribute
values. The string is used to effectively "condense" an XML tree into
a string form for comparison purposes. The idea is to test and verify
that two XML trees are identical or not.
why not use deep-equal ?

>
The rest variable then contains a similar soup of long string of
attribute names and values (of ALL the siblings). The content is then
normalized to avoid potential whitespace inconsistencies (same with
the current node) and then I simply verify that the current node in a
string form isn't found anywhere in the following siblings string
form, effectively determining that the current node and all it's
children isn't found in any of it's siblings.
> <xsl:when test="contains( $rest-normalized, $current-normalized)">

is testing if "" contains as a substring the normalized string value of
the current node, which will presumably test as false so you get
<noproblem/every time.

This seems to be a reasonable explanation for <noproblembei ng
outputted everywhere.

The magic bit though is that when you uncomment out the <xsl:value-of
select="$curren t-normalized">, which, as I understand, has nothing to
do with the evaluation of $rest against the following siblings in a
yes it has everything to do with that, as I tried to explain. the
siblings are all evaluate elements, and so if you leave the comment in
$rest is evaluated using this template and will have empty string value.
string form or $rest-normalized or the logic layed down to output
<noproblem/or <duplicate/>, the logic actually outputs <noproblem>
where I expect and <duplicateswher e I expect ;-).

If you're explanation was right, $rest being empty and by extension,
$rest-normalized being empty, it would imply that outputting value-of
$current-normalized somehow made $rest get populated correctly. That
is not what I would call expectable behavior. Or maybe I'm missing
something else?
Sorry I'm not sure how else I can explain it, but just look carefully at
your template for evaluate; it generates no output.
>
>As you are using saxon9 (thus xslt2) you probably want to be looking as
xsl:for-each-group to do duplicate removal, which is likely to be much
more efficient.

Actually using Saxon8 and we dont' really have the option of hopping
to SaxonB9 at this point in time ;-).
saxon8 is the same as 9 for this purposes they both implement teh same
version of XSLt (2.0) saxon 6 was the xslt 1 processor.
>
Regards
Jean-Francois Michaud

--
http://dpcarlisle.blogspot.com
Mar 12 '08 #10

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

Similar topics

1
564
by: Oleg Konovalov | last post by:
Hi, I am new to XSLT, trying to significantly modify somebody else's XSL. That is not 2.0. I need to create min & max variable(s) to be used in many templates and sub-templates based on certain conditions (not always). It can't be calculated at the beginning of the file
5
7628
by: Fred | last post by:
Not much expertise on XSLT and trying to understand it's uses when creating apps in VS.NET? If I wanted flexibility on the UI (View aspect of M.V.C.): - How does it compare with creating business components that can be consumed by WebForms, WinForms, mobile devices, etc? Is it even fair to compare the such technologies? - How about for cases when you need to display dynamic elements on the form/grid (as compared to knowing data elements...
3
1229
by: Fredy Muñoz [MCP] | last post by:
Hello there! I have a couple of questions about generating HTML using an XSLT Stylesheet. I use the System.Xml and System.Xml.Xsl namespaces and a XslTransform object to make the transformation, as follows: xsltTemplate.Transform(xmlOutput.CreateNavigator, Nothing, xmlWriter, Nothing)
24
1808
by: Dan Bass | last post by:
I know that XslTransform's Transform is thread safe according to the MSDN, and that Load is not. I've therefore applied this simply Mutex to it and would just like to confirm this is okay. XslTransform xslt = null; Mutex mut = new Mutex(); public override string MapMessage ( string messageSource ) {
1
1215
by: Juho Jussila | last post by:
Hi How can I easily ensure that Xml document has elements in certain order? I think it can be done with Xml schema, but I'd like to use Xslt, because the validation is a part of Xslt transformations. Xml document: <Root> <Foo/> <Bar/>
9
2148
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they should work. Also the second nav.OuterXml appears to also be wrong to me. Can someone explain to me why this does not work? (This is an example from a program we have where xpath can be entered in two parts so we have to be able
3
1913
by: Greg | last post by:
Hi, I want to create a web based interface that uses a form + Javascript (in an XHTML namespace) to construct an XPath to query and modify the attributes of some SVG (in an SVG namespace). There are lots of Google hits on each of these specifications and related technology, shuch as namespaces but there's almost nothing I can find that shows useful examples of the different ways they might be combined?
2
1280
by: MichaelRKramer | last post by:
Hi, I'm new to dotnet 2.0, but I'm an expert in asp 3 or classic asp. Most of my sites are all done with XSLT and ASP which basically writes XMLs. Now I'm reading about the new cool stuff in visual studio 2005. How does VS 2005 or .net 2.0 work with XSLT's? Is what I've been doing the best practice or a dumb ass move? Will I not be using power of 2.0? Lost programmer
1
6820
by: Arun dudee | last post by:
how to put img in xslt file where path of imge is present in xml not my pat of xml is <root> <img> <banner ban="C:/Documents and Settings/Administrator/Desktop/hariom/ xml/b.GIF"></banner> </img> <root>
0
8432
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
8764
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
8546
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
8633
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
7367
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...
0
4180
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
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1752
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.