Re: [xsl] Template matching preceding-sibling.

Subject: Re: [xsl] Template matching preceding-sibling.
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 14 Nov 2007 11:26:53 -0500
Ilya,

You may or may not hear from Scott or Charles -- contributors to this list are often busy people, and can disappear for days at a time. It's a volunteer effort after all. :-)

That being said, a wise approach is always Caveat Emptor. Don't assume that we've actually run the code we're posting. I can't speak for Scott or Charles but I post untested code fairly frequently. I'm lucky it works as often as it does.

Accordingly, you might do best to assume that a helpful contributor has made an innocent mistake, and focus instead on the XSLT issues that concern or interest you. In other words, I think your supposition that list contributors may have tried to jump forward, deduce your problem and solve it, without testing their solutions, is entirely plausible.

Similarly, I can corroborate that Mike's suggestion of match="not(activity = preceding-sibling::Rec/activity)" is more likely to be the actual solution to a real-world problem than the more elaborate match pattern you posted. But that's in the general case. Only you can say what you need in your own case.

Cheers,
Wendell

At 04:57 AM 11/14/2007, you wrote:
Michael,

thank you for your response and clarification, but you've commented on the question I haven't asked :)

My question is not about correctness of the aforementioned expression and its semantics. I'd like to know how could it be that copy-pasting the Steve's example and running it I obtained a result that was different to what he has got, and actually the result was exactly the same as he wanted to obtain (single string "Ello xslers"), if I understood his request correctly.

I might think that he just simplified his actual case to the very basic one and posted it to the list without checking the output, and in this particular case coincidently it worked according to his will. This leads me to the only conclusion that the responders (Charles, Steve and now you) were deducing the real and more general problem from its particular case description without actually running transformation, otherwise they would obviously got the same result I did, the correct one according to Steve. Am I right? If I am - then ok, I'll just construct another, more general example and will test it (that's exactly where your explanations will fit :)), but if not - it's definetely a reason to find out why one and the same template and input are tranformed on my machine into an output different to what at least several members of the list obtained.

Regards,
Ilya

PS Sorry if the meaning of my writings is hard to get - I'm not that good in English. If I described something unclear - please let me know and I'll try once more.


----- Original Message ----- From: "Michael Kay" <mike@xxxxxxxxxxxx> To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx> Sent: Wednesday, November 14, 2007 2:28 PM Subject: RE: [xsl] Template matching preceding-sibling.



You're missing that != (like the other comparison operators) returns true if any item on the left is != to any item on the right. You probably want

not(activity = preceding-sibling::Rec/activity)

but I haven't looked at the problem, it might be that you want

not(activity = preceding-sibling::Rec[1]/activity)

which only looks at the immediately preceding sibling.

Michael Kay
http://www.saxonica.com/

-----Original Message-----
From: Ilya Konanykhin [mailto:ilya.konanykhin.ml@xxxxxxxxx]
Sent: 14 November 2007 04:01
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Template matching preceding-sibling.

Hi, list!
I'm asking for clarification of the topic being on the list
several days ago (Nov 8). Steve <subsume@xxxxxxxxx> wrote:

> I'm missing something fundamental, what is it?
>
> <xsl:template match="Rec[activity !=
preceding-sibling::Rec/activity
> or not(preceding-sibling::Rec)]">  Ello xslers.
> </xsl:template>
>
> <xml>
>    <Rec>
>      <activity>hi</activity>
>    </Rec>
>    <Rec>
>       <activity>hi</activity>
>    </Rec>
> </xml>
>
> ----
>
> Above should only print anything once, but I'm getting it each time.
> What am I not getting?

Charles Knell and Scott Trenda gave advices/made notices,
thus confirming, that the problem exists. But copying the
input data and the template given by Steve and running it I
obtained exactly the result required by him in the first
topic-forming message, if I understood his request correctly.
I used MSXML (not sure about version, but don't think it's
important in this simple
case) and Saxon 9, for the latter I also tried changing
template version to 2.0 - the result stood the same. Data follows:

-----------
input.xml:
<?xml version="1.0"?>

<xml>
   <Rec>
     <activity>hi</activity>
   </Rec>
   <Rec>
      <activity>hi</activity>
   </Rec>
</xml>

-----------
input.xsl:
<?xml version="1.0"?>

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

<xsl:template match="Rec[activity !=
preceding-sibling::Rec/activity or not(preceding-sibling::Rec)]">
  Ello xslers.
</xsl:template>

</xsl:stylesheet>

-----------
output.xml:
<?xml version="1.0" encoding="UTF-8"?>

Ello xslers.


hi


-----------

As it is clearly seen the string "Ello xslers" appears only
once and not twice as Steve wrote ("hi" comes from the defaul
copy rules, as I understand it).

Did I missed or misunderstood something very basic? Can
someone please reveal the trick to me (especially interested
in Charles' oppinion as he was involved most intensively in
the discussion).

Many thanks in advance,
Ilya


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread