Subject: RE: [xsl] Selecting Nodes by nodeset comparison From: "Michael Kay" <mike@xxxxxxxxxxxx> Date: Tue, 12 Apr 2005 14:53:37 +0100 |
> 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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
AW: [xsl] Selecting Nodes by nodese, michella | Thread | [xsl] printing text output excludin, swetha d |
Re: [xsl] Extra newline remove on t, David Carlisle | Date | [xsl] Selecting only those nodes wh, Peter Hickman |
Month |