Re: [xsl] compare two node sets

Subject: Re: [xsl] compare two node sets
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 19 Jan 2020 20:49:24 -0000
Well, first of all we need to know which XSLT version we're talking about.
It's certainly going to be difficult in XSLT 1.0.

It also depends on the performance requirement. The simplest solutions may
well have quadratic performance, but since the number of directors and authors
of a film is likely to be small, that may not matter.

The simplest (XPath 2.0) solution that comes to mind is

count(//director) eq count(//author) and
every $d in //director satisfies
  some $a in //author satisfies
    deep-equal($d/*, $a/*)

Michael Kay
Saxonica

> On 19 Jan 2020, at 20:36, Wolfhart Totschnig wolfhart.totschnig@xxxxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> Hello,
>
> I have an XSL/XPath problem to which I cannot find the solution. I have an
xml file with data about films, in the following form (simplified):
>
>    <film>
>       <title>The big Lebowski</title>
>       <director>
>          <first>Joel</first>
>          <last>Coen</last>
>       </director>
>       <director>
>          <first>Ethan</first>
>          <last>Coen</last>
>       </director>
>       <author>
>          <first>Ethan</first>
>          <last>Coen</last>
>       </author>
>       <author>
>          <first>Joel</first>
>          <last>Coen</last>
>       </author>
>    </film>
>
> I would like to test whether the director(s) of the film are the same as its
writer(s) (here called "authors"). That is, I want to test whether for each
<director> element there is an <author> element with the same child elements,
and vice versa (i.e., for each <author> element a corresponding <director>
element). So, in the above case, the test should return <true>. Note that the
order of the <director>/<author> elements should not matter. By contrast, in
the following example the test should return <false>:
>
>    <film>
>       <title>M</title>
>       <director>
>          <first>Fritz</first>
>          <last>Lang</last>
>       </director>
>       <author>
>          <first>Thea von</first>
>          <last>Harbou</last>
>       </author>
>       <author>
>          <first>Fritz</first>
>          <last>Lang</last>
>       </author>
>    </film>
>
> How can this be done?
>
> To explain the purpose of this: When the test comes out true, the output
will be "Written and directed by [names]", whereas otherwise it will be
"Written by [names]. Directed by [names]".
>
> Thanks in advance for your help!
>
> Wolfhart

Current Thread