Subject:parsing error using Author:allen watson Date:18 Nov 2005 01:12 PM Originally Posted: 17 Nov 2005 08:31 PM
in a "for-each" block of code I am testing multiple Product Numbers. I get the same bad result if I use a "xsl:when" or "xsl:if". The following line works only on the first product number and fails on all the following product numbers. <xsl:when test="substring(field,1,7) = '1234567'"/>. I need to test just the first 7 characters. I have a many-to-many test to do. The following line works on every product number <xsl:when test="field = '1234567'"/> but I need just the first 7 characters. Is there a fix or a creative workaround.
See attached samples.
Subject:parsing error using Author:(Deleted User) Date:18 Nov 2005 07:04 AM
I'm sorry you haven't given me enough to go on.
Can you extract the critical part of the xslt into a little xslt that runs by itself and make a small well-formed sample input file that shows the problem?
Subject:parsing error using Author:(Deleted User) Date:21 Nov 2005 12:46 PM
You have encountered a subtle difficulty when trying to compare nodesets to strings.
This is your sample data:
In your .xslt file, you have
<xsl:if test="substring(field,1,6) = 'KRL761'">
<xsl:if test="substring(field,1,7) = 'KRL7002'">
The xsl:for-each executes once for each ProductNo element, NOT once for each ProductNo/field.
The way you have written the .xslt, field is not a single string value, but a nodeset containing all the field nodes in the ProductNo element.
Now, in XPATH, an expression like nodeset=’string’ is defined to be true if ANY node in nodeset matches ‘string’, so your program works pretty well as long as you stick to simple equality. However, in XPATH, the substr(…) function is defined differently. If its first argument is a nodeset, substr takes the string value of the FIRST node in the nodeset and discards the rest. Since KRL1024PC is not the first node in the nodeset, substr(...) = ‘KRL1024’ will never match properly.
I don’t know whether you intended to use nodesets, or if it was an accidental error, but here is a suggestion on how to fix the program: