[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Selecting Nodes by nodeset comparison

Subject: RE: Selecting Nodes by nodeset comparison
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 12 Apr 2005 14:53:37 +0100
xslt nodeset comparison
> XML1 :
>
> <EintrdgeListe>
> <Eintrag Datum="20050212" Zeit="0800"	Menge="64"/>
> <Eintrag Datum="20050214" Zeit="0800" Menge="36"/>
> <Eintrag Datum="20050214" Zeit="0800" Menge="22"/>
> <Eintrag Datum="20050214" Zeit="0800" Menge="2"/>
> </EintrdgeListe>
>
>
> XML2 :
>
> <EintrdgeListe>
> <Eintrag BZ_Abgangsdatum="20050211" BZ_Zeit="0800"
> BZF_MengeGezdhlt="147"/>
> <Eintrag BZ_Abgangsdatum="20050211" BZ_Zeit="0800"
> BZF_MengeGezdhlt="50"/>
> <Eintrag BZ_Abgangsdatum="20050212" BZ_Zeit="0800"
> BZF_MengeGezdhlt="49"/>
> <Eintrag BZ_Abgangsdatum="20050213" BZ_Zeit="0800"
> BZF_MengeGezdhlt="37"/>
> </EintrdgeListe>
>
> -----------
> Is the following Xpath correct?
>
> <xsl:variable
> 	name="matching_XML1"
>
> select="$XML1/Eintrag[@Datum=XML2/Eintrag/@BZ_Abgangsdatum
> and substring(@Zeit,1,2)=substring($XML2/Eintrag/@BZ_Zeit,1,2)]"
> />

Three problems with this:

(1) you'll get a match if there is an Eintrag with a matching Abgangsdatum
and a *different* Eintrag with a matching Zeit. You want them to match on
the same Eintrag.

(2) although NS1=NS2 returns true if there is any node in NS1 that's equal
to a node in NS2, the same isn't true for s(NS1)=s(NS2) where s() is a
function such as substring() that returns a string. substring(NS) doesn't
give you a set of strings, it gives you the string value of the first node
in NS.

(3) it's awfully expensive if there are many Eintrage. O(N*M) where N and M
are the sizes of the two files.

Solution to all 3 problems: use keys.

<xsl:key name="k" match="Eintrag" use="concat(@BZ_Abgangsdatum, 'T',
@substring(@BZ_Zeit, 1, 2))"/>

In XSLT 2.0 you can then select all the entries in $XML1 that have a
matching entry in $XML2 using

select="$XML1/Eintrag[key('k', concat(@Datum, 'T', substring(@Zeit, 1, 2)),
$XML2)]"

It's slightly more difficult in 1.0 because key() only selects nodes from
the context document, and you need to refer to both documents. So long as
you don't absolutely need the results in a variable, you can do

<xsl:for-each select="$XML1/Eintrag">
  <xsl:variable name="key" select="concat(@Datum, 'T', substring(@Zeit, 1,
2))"/>
  <xsl:variable name="e" select="."/>
  <xsl:for-each select="$XML2">
    <xsl:if test="key('k', $key)">
      ... process $e ..
    </xsl:if>
  </xsl:for-each>

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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.