XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
allen watsonSubject: 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.


UnknownSample(4).xml
Sample xml

UnknownSample(4).xsl
sample xsl

Postnext
(Deleted User) Subject: parsing error using
Author: (Deleted User)
Date: 18 Nov 2005 07:04 AM
Hi Allen,
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?

Thanks
- clyde

Posttop
(Deleted User) Subject: parsing error using
Author: (Deleted User)
Date: 21 Nov 2005 12:46 PM
Allen,

You have encountered a subtle difficulty when trying to compare nodesets to strings.

This is your sample data:
<?xml version="1.0"?>
<Data xmlns:xfdf="http://ns.adobe.com/xfdf-transition/">
<P3>
<AppendixA>
<ProductNo>
<field>KRA213D</field>
<field>KRL1024PC</field>
</ProductNo>
</AppendixA>
</P3>
</Data>

In your .xslt file, you have
<xsl:for-each select="*/AppendixA/ProductNo">
<xsl:if test="substring(field,1,6) = 'KRL761'">
<xsl:value-of select="1"/>
</xsl:if>
<xsl:if test="substring(field,1,7) = 'KRL7002'">
<xsl:value-of select="1"/>
</xsl:if>
etc…

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:

<xsl:for-each select="*/AppendixA/ProductNo/field">
<xsl:if test="substring(.,1,6) = 'KRL761'">
<xsl:value-of select="1"/>
</xsl:if>
<xsl:if test="substring(.,1,7) = 'KRL7002'">
<xsl:value-of select="1"/>
</xsl:if>
etc…



I hope this clarifies the issue,
- clyde

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.