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 March 26th, 2008, 11:03 AM
Authorized User
 
Join Date: Mar 2008
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Whats the work around?

How would I go about to use the name of and XML element for the value of an attribute in an attribute-set? Because XSLT isn't like the java I'm used to, I can't just create a variable and change the value based on ".", could someone please advise? thanks...

<xsl:template match="/root">
 <xsl:for-each select"*">
  <xsl:element name="newElement" use-attribute-set="newElementAtts">
   <xsl:value-of select="."/>
  </xsl:element>
 </xsl:for-each>
</xsl:template>



<xsl:attribute-set name="newElementAtts">
 <xsl:attribute name="theName">(how do I get the name of the calling element here?)</xsl:attribute>
</xsl:attribute-set>

 
Old March 26th, 2008, 11:11 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

An attribute set is a fixed set of attributes which I don't believe have any concept of context.

A similar concept might be accomplished by either using a named template or template modes:

Code:
<xsl:template match="/root">
  <xsl:for-each> 
    <xsl:element name="newElement">
     <xsl:call-template name="defaultAttributes"/>
     <xsl:value-of select="."/>
    </xsl:element>
  </xsl:for-each>
</xsl:template>

<xsl:template name="defaultAttributes">
  <xsl:attribute name="theName"><xsl:value-of select="name()"/></xsl:attribute>
</xsl:template>
However, if you are only doing the one attribute then this might be a much quicker way of doing it:

Code:
<xsl:template match="/root">
  <xsl:for-each> 
    <newElement theName="{name()}">
     <xsl:value-of select="."/>
    </newElement>
  </xsl:for-each>
</xsl:template>
/- Sam Judson : Wrox Technical Editor -/
 
Old March 26th, 2008, 12:07 PM
Authorized User
 
Join Date: Mar 2008
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Excellent. I think what I was looking for was the "name()" command/function/whatever....

Thanks for taking the time to answer my question I will try your examples or variations of them ....


 
Old March 26th, 2008, 12:13 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

<xsl:attribute-set name="newElementAtts">
  <xsl:attribute name="theName">
    <xsl:value-of select="name(.)"/>
  </xsl:attribute>
</xsl:attribute-set>


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 26th, 2008, 12:27 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>An attribute set is a fixed set of attributes which I don't believe have any concept of context.

Actually, it's rather unusual to take advantage of the fact, but the context item is defined when evaluating an attribute set, so it is possible to construct attributes whose value is context-dependent.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 26th, 2008, 04:10 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

So much for my believe system :(

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Whats this book like? PaulBerry BOOK: Beginning C# 2005 Databases 0 January 25th, 2008 12:55 PM
Whats this error rajanikrishna ASP.NET 2.0 Basics 0 February 17th, 2006 02:10 AM
Codegroup Whats it? JelfMaria VS.NET 2002/2003 3 July 8th, 2005 09:43 AM
Whats wrong? Agentofnight Beginning PHP 3 April 17th, 2005 04:11 AM
Whats the diff?? nsakic XSLT 2 January 17th, 2004 07:11 PM





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