Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 3rd, 2007, 02:53 PM
Registered User
 
Join Date: Sep 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML + XSLT Compare Nodes

Okay,

Very simple for some of you I'm sure. I want to compare to node values and if they don't match apply a class from my style sheet. I know how to apply the css class, but what I can't do is compare the nodes. Can anyone help me with that. Below is sample data:

In this sample I want to test and see if MentorQty = EBOMQty.

Code:
<?xml version="1.0" encoding="utf-8"?>
<BOMResolutionReport>   
<ComponentList>
      <Component>
         <Line>
            <MentorFindNumber>106</MentorFindNumber>
            <MentorQty>1.0</MentorQty>
            <MentorComponentName>A-0000200-01</MentorComponentName>
            <EBOMFindNumber>106</EBOMFindNumber>
            <EBOMQty>1.0</EBOMQty>
            <EBOMComponentName>A-0000200-01</EBOMComponentName>
            <EBOMDescription>Test Part for Master Mapping</EBOMDescription>
            <FormPer>the form per</FormPer>
            <SeeView>the note</SeeView>
            <SeeNote>the view</SeeNote>
            <Caution>this is the caution</Caution>   
            <Status>Full Match</Status>
            <MappingUsed>Master</MappingUsed>
         </Line>

      </Component>
</BOMResolutionReport>
Can anyone help me with this? Many Thanks if you can!!

Here is my general logic:

Code:
<xsl:if test="BOMResolutionReport/ComponentList/Component/Line/MentorQty = BOMResolutionReport/ComponentList/Component/Line/EBOMQty">
      <xsl:attribute name="class">yellowbg</xsl:attribute>
</xsl:if>
 
Old September 3rd, 2007, 03:21 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>In this sample I want to test and see if MentorQty = EBOMQty.

If Line is the context item you can do

<xsl:if test="MentorQty = EBOMQty">

If you tried that and it didn't work, you probably got the context wrong; but one can't really tell what was wrong with your code without seeing it.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 3rd, 2007, 03:27 PM
Registered User
 
Join Date: Sep 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is my code:
Code:
<td>
   <xsl:for-each select="MentorQty">
      <xsl:choose>
         <xsl:when test="node()[not(.='null')]">
            <xsl:choose>
               <xsl:when test="MentorQty = EBOMQty">
                  <xsl:attribute name="class">yellowbg</xsl:attribute>
               </xsl:when>
            </xsl:choose>

            <xsl:apply-templates />
         </xsl:when>

         <xsl:otherwise>N/A</xsl:otherwise>
      </xsl:choose>
   </xsl:for-each>
</td>
I don't get any error messages, but I don't get anything with the yellowbg class applied either. I know the xsl:attribute works fine because I use it on other parts of this file.

 
Old September 4th, 2007, 01:30 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

When you do <xsl:for-each select="MentorQty"> the context changes from the Line element to the MentorQty element, so the comparison needs to become test=". = ../EBOMQty"

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 4th, 2007, 07:05 AM
Registered User
 
Join Date: Sep 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok,

I found a fix for my problem. I had my test for MentorQty != EBOMQty in wrong place.

Code:
<td>
   <xsl:if test="EBOMQty != MentorQty">
      <xsl:attribute name="class">yellowbg</xsl:attribute>
   </xsl:if>

   <xsl:for-each select="EBOMQty">
      <xsl:choose>
         <xsl:when test="node()[not(.='null')]">
            <xsl:apply-templates />
         </xsl:when>

         <xsl:otherwise>0</xsl:otherwise>
      </xsl:choose>
   </xsl:for-each>
</td>
By moving it outside of my xsl:for-each it worked. I have no idea why, it just did.

 
Old September 5th, 2007, 03:18 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>By moving it outside of my xsl:for-each it worked. I have no idea why, it just did.


The fact that you still don't understand the cause is depressing, given that I explained to you that it was because xsl:for-each changed the context. Please don't just use trial and error - read the sections on XPath context and xsl:for-each in your favourite textbooks, and ask questions here if you don't understand them.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT 1.0: Looping through nodes kwilliams XSLT 4 December 1st, 2008 06:21 PM
Compare values in different nodes fann XSLT 9 September 11th, 2008 06:25 AM
XSLT Help to get value from Upper Node by compare pixelheart XSLT 10 May 1st, 2008 12:25 PM
Compare two xml files using xslt sudha XSLT 0 March 10th, 2006 01:04 AM
How to compare nodes from 2 XML docs kwilliams XSLT 0 August 30th, 2005 01:43 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.