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 February 25th, 2011, 12:15 PM
Friend of Wrox
 
Join Date: May 2004
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default Variables and the 'to' construct

Using Saxon 9 in Win 7/64

I have a simple enough XML

Code:
<xsl:variable name="myData">
<AAA>ABC<BBB><CCC>1</CCC><DDD>5</DDD></BBB>XYZ</AAA>
</xsl:variable>
I want to process this so I call a template

Code:
<xsl:call-template name="myTemplate">
<xsl:with-param name="myData" select="$myData">
</xsl:call-template>
My template is

Code:
<xsl:template name="myTemplate">
<xsl:param name="myData"/>


<xsl:for-each select="$myData//BBB/CCC to $myData//BBB/DDD">
<xsl:value-of select="."/>
</xsl:for-each>
What I expected to see are the numbers 1 2 3 4 5, which I do get.

But, I get is an error message:

Quote:
Validate error at xsl:for-each on line (line #) of file (filename)
FORG0001: Cannot convert zero-length string to an integer
Transformatin failed: Run-time errors were reported
What zero-length string are we talking about here?

Thanks,

- m
__________________
------------------------
Keep Moving Forward

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

Michael Hare
 
Old February 25th, 2011, 12:21 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

It outputs 12345 when I run it on Saxon 9.1.0.2.

I suspect one or more of your $myData variables is out of scope, so not being passed into the template correctly perhaps?
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old February 25th, 2011, 12:25 PM
Friend of Wrox
 
Join Date: May 2004
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default

This is why I love this forum. It's like talking to a therapist. As soon a you post, you begin to discover the cure.

I added text() to the end of the select items for the for-each statement
Code:
<xsl:for-each select="$myData//BBB//C/text() to $myData//BBB//DDD/text()">
and that has fixed the issue.

I'm not sure why it silenced the error message as I still get my 1 2 3 4 5 I was expecting.

Thanks for the venue! It really helps!
__________________
------------------------
Keep Moving Forward

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

Michael Hare
 
Old February 25th, 2011, 12:26 PM
Friend of Wrox
 
Join Date: May 2004
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default

Thanks, Sam. I posted before I saw your response.
I get 1 2 3 4 5 in the first version, I just also get the error message.

Adding text() to the select statement silenced the error message, but I don't know why.

Thanks!
__________________
------------------------
Keep Moving Forward

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

Michael Hare
 
Old February 25th, 2011, 02:08 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I can't see any reason why adding /text() should make a difference. However, you also changed the element name from CCC to C. There's no way C in a path expression will match CCC in your data, so you're not telling us the whole truth...
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old February 25th, 2011, 03:00 PM
Friend of Wrox
 
Join Date: May 2004
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default

I agree, C was a typo, I meant CCC
But I do believe I'm leaving out some important point, but I don't know what it is.

I distilled the XML/XSLT down to something I could easily post, in reality there is more going on. Namepsaces, for example. I have to contend with a few namespaces when I'm doing the transformation. When I create $myData, it is in a namespace: xmlns="http://software/nmitl1", so when I define myTemplate, I give it xpath-default-namespace="http://software/nmitl1". That alone caused me much grief until I remembered I had to deal with the namespace issue.

The call to myTemplate actually happens inside another template

Code:
<xsl:template name="myOtherTemplate" xpath-default-namepsace="http://software/nmitl1">
<xsl:param name="myData"/>

<xsl:if test="exists($myData//BBB)">
<xsl:call-template name="myTemplate">
<xsl:with-param name="myData" select="$myData"/>
</xsl:call-template>
</xsl:if>

</xsl:template>
I didn't think this was important since it essentially just passes myData through to myTemplate. Eventually, more processing will go on in this template.

The way I set the values for CCC and DDD are also a bit convoluted.
I have to follow a cross reference to other parts of the input document to find the values for CCC and DDD. These are stored in a temporary result and xpath'd out later to fill in CCC and DDD using something like
Code:
<xsl:variable name="myData">
<AAA>ABC<BBB>
<CCC><xsl:value-of select="$tmpData//tmpCCC"/></CCC>
<DDD><xsl:value-of select="$tmpData//tmpDDD"/></DDD>
</BBB>XYZ</AAA>
When I call to myOtherTemplate, I do it in a variable as I am wanting to capture the output in a temporary result for later use.
Code:
<xsl:variable name="myResult">
<RESULT>
<xsl:call-template name="myOtherTemplate">
<xsl:with-param name="myData" select="$myData"/>
</call-template>
</RESULT>
</xsl:variable>
I wasn't trying to leave important stuff out, I was just trying to distill the problem down to, what I thought was, a minimal series of events.

Thanks again!
__________________
------------------------
Keep Moving Forward

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

Michael Hare
 
Old February 25th, 2011, 03:31 PM
Friend of Wrox
 
Join Date: May 2004
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default

It has something to do with the way I'm filling in the CCC and DDD elements.
If I just put numbers in there, like 3 and 7, then the templates work without the text() elements.

Wished I would have thought to try that before posting here. But, like I said, this place is therapeutic and helps to clear thoughts when writing them down.

I may be back asking for help on the reference following.

Thanks again!
__________________
------------------------
Keep Moving Forward

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

Michael Hare
 
Old February 26th, 2011, 05:19 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>I was just trying to distill the problem down to, what I thought was, a minimal series of events.

Yes, I know. When you spend a lot of time answering questions, typos like that are a very good clue that you're not seeing the real data but some abstraction of it. Abstracting and simplifying the problem is a good thing to do: but you should always simplify it to something that is self-contained, complete, and demonstrates the problem. The test is: could the reader reproduce the problem from the information given? If there's a typo, it means the code being posted probably hasn't been executed at all, which means that looking for bugs in it is a waste of time.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Construct checkbox with Javascript BananaJim Javascript How-To 4 April 30th, 2012 02:32 AM
is there a similar construct to if-statements in xsd? dhsmd XML 2 March 24th, 2009 08:59 AM
Construct a SQL Select Statement ricespn Beginning VB 6 1 May 28th, 2007 07:57 AM
Use variables or other construct? jacob XSLT 4 November 15th, 2005 08:06 AM





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