xsl-list
[Top] [All Lists]

Re: [xsl] How to compare two sequences, where order matters and length matters?

2017-11-21 17:42:19
The deep-equal() function MIGHT meet your requirement. But your requirement 
isn't described in enough detail to be sure (you've only specified it by 
showing examples of some operands that are equal and some that aren't). For a 
full specification you need to say how to handle comments, whitespace, in-scope 
namespaces, base URI, schema type annotations, etc etc etc.

Michael Kay
Saxonica

On 21 Nov 2017, at 23:21, Costello, Roger L. costello(_at_)mitre(_dot_)org 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Hi Folks,

Consider this XML:

<sequences>
   <sequence/>
   <sequence>
       <item>A</item>
   </sequence>
   <sequence>
       <item>B</item>
   </sequence>
   <sequence>
       <item>A</item>
       <item>A</item>
   </sequence>
   <sequence>
       <item>A</item>
       <item>B</item>
   </sequence>
   <sequence>
       <item>B</item>
       <item>A</item>
   </sequence>
   <sequence>
       <item>B</item>
       <item>B</item>
   </sequence>
</sequences>

Suppose that $item has this value:

   <item>A</item>

And suppose the root element, <sequences>, is the context node.

Then, this evaluates to true (thanks David):

   sequence[2] = (sequence[1], $item)

Unfortunately, this also evaluates to true:

   sequence[4] = (sequence[1], $item)

sequence[4] is this:

   <sequence>
       <item>A</item>
       <item>A</item>
   </sequence>

And this also evaluates to true:

   sequence[5] = (sequence[1], $item)

sequence[5] is this:

   <sequence>
       <item>B</item>
       <item>A</item>
   </sequence>

Eek!

I don't want that. I only want these two sequences to match: sequence[2] and 
(sequence[1], $item).

For this comparison:

   Blah/item *compare-operator* (Bar/item, $item)

It should only return true if:

1. The number of <item> elements are the same on the left and right side of 
compare-operator.

2. The value of item[1] of the left sequence equals the value of item[1] of 
the right sequence.
The value of item[2] of the left sequence equals the value of item[2] of the 
right sequence.
Etc.

In other words, in the comparison the order of the items in the left and 
right sequences matters and the number of the items in the left and right 
sequences matter. 

What XPath expression will do such a comparison? 

Note: I'd like the XPath expression to *not* use a for-each loop.

/Roger 


--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>