xsl-list
[Top] [All Lists]

RE: Selecting Nodes by nodeset comparison

2005-04-12 06:53:37
XML1 :

<EinträgeListe>
<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"/>
</EinträgeListe>


XML2 :

<EinträgeListe>
<Eintrag BZ_Abgangsdatum="20050211" BZ_Zeit="0800"    
BZF_MengeGezählt="147"/>
<Eintrag BZ_Abgangsdatum="20050211" BZ_Zeit="0800"  
BZF_MengeGezählt="50"/>
<Eintrag BZ_Abgangsdatum="20050212" BZ_Zeit="0800"    
BZF_MengeGezählt="49"/>
<Eintrag BZ_Abgangsdatum="20050213" BZ_Zeit="0800"    
BZF_MengeGezählt="37"/>
</EinträgeListe>

-----------
Is the following Xpath correct?

<xsl:variable 
      name="matching_XML1" 
      
select="$XML1/Eintrag[(_at_)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/  



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



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