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 June 21st, 2010, 01:04 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2010
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default using xsl:attribute with xsl:for-each

Hi,

I am attempting to use an xsl:attribute for the the select value of xsl:for-each. The code of my attempt is below.

Code:
<xsl:for-each >
<xsl:attribute name="select">
data/<xsl:value-of select="$XMLNodeName_announcements"/>/announcement
</xsl:attribute>
This fails and complains "Warning: XSLTProcessor::importStylesheet() [xsltprocessor.importstylesheet]: xsl:for-each : select is missing in ". After doing some research I found that XSLT does not support dynamic evaluation of xpaths. I was wondering if someone has an ingenious idea of a work around. http://stackoverflow.com/questions/2...elect-statment has some information for a related problem, but I had trouble applying the knowledge of that post to my situation. One of the design requirements of the application I am working on is that XML nodes names are configurable.

Any help is appreciated. Thanks!
 
Old June 21st, 2010, 01:11 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Imaginative, but completely wrong. xsl:attribute creates an attribute node in the result tree, not in the stylesheet.

If you want to construct an XPath expression as a string at run-time and then evaluate it, you need an extension such as saxon:evaluate().

On the other hand if the variable $XMLNodeName_announcements simply holds an element name, you can do
Code:
<xsl:for-each select="data/*[name()=$XMLNodeName_announcements]"/>
.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference

Last edited by mhkay; June 21st, 2010 at 01:12 PM.. Reason: typo
The Following User Says Thank You to mhkay For This Useful Post:
mkzan (June 21st, 2010)
 
Old June 21st, 2010, 02:54 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2010
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Obviously my understanding of XSLT is a little off. This project I am on is the first experience I have with XSLT.

The solution you have for *[name()=$XMLNodeName_announcements] works great. Thanks for the help. If you don't mind clarifying, what is the significance of the * ?
 
Old June 21st, 2010, 04:22 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

select="*" would match all child elements of the context node. Adding a predicate in square brackets restricts it to those children that match the predicate, in this case a test on the name.
__________________
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
xsl:function pass param to attribute of xsl:instruction bonekrusher XSLT 4 March 31st, 2009 09:06 AM
Excluding attribute from xsl:coy-of ricksj XSLT 1 October 6th, 2008 04:56 PM
XSL Attribute ID jonty XSLT 1 March 28th, 2006 03:53 PM
onClick as xsl attribute? mlaba XSLT 1 September 10th, 2004 02:36 AM
XSL Transform with xsl string NOT xsl file skin XSLT 0 June 16th, 2003 07:30 AM





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