0
<my-courses>
  <courses-reqd>
     <details> 
       <subject>Math</subject>
     </details>
     <details>
       <subject>Econ</subject>
     </details>
     <details>
       <subject>Geog</subject>
     </details> 
     <details>
       <subject>Phys</subject>
     </details>
     <detail>
       <subject>Zool</subject>
     </details>
  </courses-reqd>
  <courses-taken>
    <details> 
      <subject>Math</subject>
    </details>
    <details>
      <subject>Econ</subject>
    </details>
    <detail>
      <subject>Zool</subject>
    </details>
  </courses-taken>

I have provided sample XML and want to know how to get the values the are not in the courses-taken but are in the courses-reqd by using XPATH. In the case provided, the answer would be Geog and Phys as they do not exist in course-taken.

<xsl:apply-templates select="my-courses\courses-reqd | not(my-courses\courses-taken)"/>

Is this possible and if so, how would I go abouts doing this. Any help would be appreciated.

Thanks

Moxie C
  • 442
  • 1
  • 15
  • 32

1 Answers1

0

The simple answer here is to do this:

<xsl:apply-templates
   select="/my-courses/courses-reqd/detail
                [not(subject = /my-courses/courses/taken/details/subject)]" />

A better (more efficient) approach is to use keys.

This would be added towards the top of your XSLT:

<xsl:key name="kTaken" match="courses-taken//subject" use="."/>

And then this would be used in one of your templates:

<xsl:apply-templates 
         select="/my-courses/courses-reqd/detail[not(key('kTaken', subject))]" />

When this XSLT is run on your sample input (after the sample input has been corrected to consistently use elements named "details" and not "detail":

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:key name="kTaken" match="courses-taken//subject" use="."/>

  <xsl:template match="/">
    <root>
      <xsl:apply-templates 
          select="/my-courses/courses-reqd/details[not(key('kTaken', subject))]" />
    </root>
  </xsl:template>

  <xsl:template match="details">
    <course>
      <xsl:value-of select="subject"/>
    </course>
  </xsl:template>
</xsl:stylesheet>

The result is:

<root>
  <course>Geog</course>
  <course>Phys</course>
</root>
JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • Thanks for the response. I have tried it and it works. I have added a subj-no tag to each of the subjects How would I code it if it is looking for two keys for instance, subject and subj-no. I have tried concatenating this by doing the following: However, I have had no luck. Any suggestions on what I am doing wrong? – Moxie C Feb 04 '13 at 23:35