xsl-list
[Top] [All Lists]

Re: Comma Separated Values - XSLT1.0

2004-10-21 02:53:03
Hi Allan,

I have 2 XML elements each containing comma separated values. I need to
take each individual value from <elem2> and see if it exists in <elem1>. If
true is returned, go off and do something..... otherwise get the next value
from <elem2> and compare it to all the values in <elem1> etc etc.

<data>
     <elem1>bloggs,smith,jones,bush,howard,bennis</elem1>
     <elem2>skeen,seinfield,longshaw,bloggs,keitel</elem2>
</data>
Of course, you could simplify the problem by first converting your input to something more XMLish:

<data>
   <elem1>
       <e>bloggs</e>
       <e>smith</e>
       <e>jones</e>
       <e>bush</e>
       <e>howard</e>
       <e>bennis</e>
   </elem1>
   <elem2>
       <e>skeen</e>
       <e>seinfield</e>
       <e>longshaw</e>
       <e>bloggs</e>
       <e>keitel</e>
   </elem2>
</data>

With Jarno's tokenizer template you could generate such an intermediate file and then take that as input for a second transform:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
   <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
   <xsl:key name="e1" match="elem1/e" use="."/>
   <xsl:template match="/*">
       <result>
           <xsl:apply-templates select="elem2/e"/>
           <xsl:if test="not(elem1/e = elem2/e)"><not-found/></xsl:if>
       </result>
   </xsl:template>
   <xsl:template match="elem2/e">
       <xsl:apply-templates select="key('e1',.)"/>
   </xsl:template>
   <xsl:template match="elem1/e">
       <found><xsl:value-of select="."/></found>
   </xsl:template>
</xsl:stylesheet>

that would give <found>bloggs</found> (or <not-found/> if nothing is found).

Note: test="not(elem1/e = elem2/e)" does not mean "the node-sets elem1/e and elem2/e are different", but "there is no elem1/e element that has the same string value as any elem2/e element".

HTH,
Anton


So the approach would be:

Take 'skeen' and compare it to all the comma separated values in <elem1>.
If FALSE is returned, take 'seinfield' and compare to all values in <elem1>
and so on...If TRUE is returned at any stage DO SOMETHING. If at the end
nothing is found, DO SOMETHING DIFFERENT......


Any suggestions on how to do this?

Thanks!


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