Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old April 6th, 2010, 05:01 AM
Authorized User
Points: 75, Level: 1
Points: 75, Level: 1 Points: 75, Level: 1 Points: 75, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2010
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
Default Saxon 9HE and xquery for loops

Hi,

according to http://saxon.sourceforge.net/#F9.2HE saxon home edition supports the XQuery 1.0 final spec; but I can't get the following to work:
Code:
 
<xsl:for-each select="for $x in ('a','b','c') where 1=1 return $x">
  <xsl:message select="."/>
</xsl:for-each>
If I try to compile that snippet I get:
Code:
Error at xsl:for-each on line 28 column 74 of testing.xsl:
  XPST0003: XPath syntax error at char 24 on line 28 in {for $x in ('a','b','c') where ...}:
    expected "return", found "null"
Failed to compile stylesheet. 1 error detected.
Is there something wrong with my xquery expression (I am look at this since an hour or so but can't figure it out, might be missing something obvious)?

Thanks in advance,
Florian
Reply With Quote
  #2 (permalink)  
Old April 6th, 2010, 05:12 AM
mhkay's Avatar
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 Posts
Default

Saxon supports either XSLT or XQuery but not both at once! The expressions used in the select attribute of an XSLT instruction must always be XPath expressions, not XQuery expressions. (Saxon would have no difficulty supporting what you attempted, but it's not permitted by the W3C specifications.)

In fact Saxon does allow XSLT and XQuery to be mixed, but only by cross-language function calling. There's a declaration saxon:import-query that allows you to import an XQuery function library into an XSLT stylesheet, after which you can call the functions in that module as if they had been written in XSLT.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
The Following User Says Thank You to mhkay For This Useful Post:
apollo13 (April 6th, 2010)
  #3 (permalink)  
Old April 6th, 2010, 05:24 AM
Authorized User
Points: 75, Level: 1
Points: 75, Level: 1 Points: 75, Level: 1 Points: 75, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2010
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Ah thx. saxon:import-query is PE onwards only right? I solved that problem now by using an xsl:if inside the for-loop, but I guess it would be nicer to use the professional edition. On the other hand; I do hate license files… keeping track of 100++ license files is something I don't want to do either ;)

Thx again,
Florian
Reply With Quote
  #4 (permalink)  
Old April 6th, 2010, 05:29 AM
mhkay's Avatar
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 Posts
Default

If you can solve the problem using an xsl:if inside the for loop, then you can probably solve it with a simple predicate:

<xsl:for-each select="expr[condition]">
....
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #5 (permalink)  
Old April 6th, 2010, 05:48 AM
Authorized User
Points: 75, Level: 1
Points: 75, Level: 1 Points: 75, Level: 1 Points: 75, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2010
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Hi,

yeah I did try that, but my xslt/xpath knowledge isn't good enough. That does work:
Code:
      <xsl:for-each select="('fontcolor','fontsize','c')">
        <xsl:if test="$table/col/@*[local-name() = current()]">
          <xsl:attribute name="{.}" select="$table/col/@*[local-name() = current()]"/>
        </xsl:if>
      </xsl:for-each>
but I am not able to move that up into the for-each select attribute.

Florian
Reply With Quote
  #6 (permalink)  
Old April 6th, 2010, 06:05 AM
Friend of Wrox
Points: 6,673, Level: 34
Points: 6,673, Level: 34 Points: 6,673, Level: 34 Points: 6,673, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,242
Thanks: 0
Thanked 244 Times in 243 Posts
Default

You have not described exactly what you want to achieve but I think that for-each you posted can simply be replaced by
Code:
<xsl:copy-of select="$table/col/(@fontcolor, @fontsize, @c)"/>
as it seems you simply want to copy those three attributes.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #7 (permalink)  
Old April 6th, 2010, 06:13 AM
mhkay's Avatar
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 Posts
Default

I think Martin's solution works for this example.

More generally, you're doing a join here, and joins can be a bit tricky with XPath alone, but it can generally be done. If your attribute names weren't constant, for example, you could do

Code:
<xsl:for-each select="$name[some $a in $table/col/@* satisfies local-name($a) eq .]">
or more simply, exploiting the semantics of "=" when comparing sequences,

Code:
<xsl:for-each select="$name[. = $table/col/@*/local-name()]">
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #8 (permalink)  
Old April 6th, 2010, 07:02 AM
Authorized User
Points: 75, Level: 1
Points: 75, Level: 1 Points: 75, Level: 1 Points: 75, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2010
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Martin Honnen View Post
You have not described exactly what you want to achieve but I think that for-each you posted can simply be replaced by
Code:
<xsl:copy-of select="$table/col/(@fontcolor, @fontsize, @c)"/>
as it seems you simply want to copy those three attributes.
Well I don't want to copy them (at least only to some extend), I want to take those three values out of the xml data [in this example simplified as $table] and, if defined, put them as attribute on fo:block [missing in my example]. So essentially I want to copy some attributes from the data source onto attributes of the output (xsl:fo in this case, later to be processed via apache fop).

Quote:
Originally Posted by mhkay View Post
or more simply, exploiting the semantics of "=" when comparing sequences,
Code:
<xsl:for-each select="$name[. = $table/col/@*/local-name()]">
That one is really nice, and exactly what I need in thise case…

Thanks to both of you,
Florian
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Xalan v. Saxon and Xquery Camer XSLT 5 May 26th, 2009 12:44 PM
break for-each loops, or limit amount of loops warhero XSLT 2 July 4th, 2007 02:18 AM
How to get a XQuery.msi ? goldensaying XML 1 December 13th, 2006 12:01 PM
While loops and For loops in XSLT spencer.clark XSLT 1 August 5th, 2005 09:50 AM
XQuery sonicDace XML 2 March 19th, 2004 04:45 PM



All times are GMT -4. The time now is 04:41 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.