XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old June 1st, 2003, 07:57 AM
torrent torrent is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Location: UK
Posts: 59 torrent User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
XSLT: can't get {local-name()} to work

Okay, I've just started with XSLT this weekend. I bought a book and read some articles and am just starting to really get into it.

I have a question. Purely for the purpose of an exercise I wish to iterate through an XML document and convert some child elements to attributes for a parent element. I know it's generally bad-practice to go overboard with attributes but I am doing this purely as an exercise, not for any real-life purpose.

As an example I would like this...
Code:
<name>
    <first>Fred</first>
    <middle>J.</first>
    <last>Bloggs</last>
</name>
...to become...
Code:
<name first="Fred" middle="J." last="Bloggs" />
Here's the XML document
Code:
<?xml version="1.0" encoding="UTF-8"?>
<people>
    <name>
        <first>John</first>
        <middle>Fitzgerald Johansen</middle>
        <last>Doe</last>
    </name>
    <name>
        <first>Franklin</first>
        <middle>D.</middle>
        <last>Roosevelt</last>
    </name>
    <name>
        <first>Alfred</first>
        <middle>E.</middle>
        <last>Neuman</last>
    </name>
    <name>
        <first>John</first>
        <middle>Q.</middle>
        <last>Public</last>
    </name>
    <name>
        <first>Jane</first>
        <middle/>
        <last>Doe</last>
    </name>
</people>
and here's the Stylesheet
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml" encoding="UTF-8" />

<!-- root document template -->
<xsl:template match="/">
    <people>
        <!-- execute any templates defined for elements which are a child of <people> -->
        <xsl:apply-templates />
    </people>
</xsl:template>

<!-- match any <name> elements -->
<xsl:template match="name">
    <!-- create an element called name and use the attribute set called attribs -->
    <xsl:element name="name" use-attribute-sets="attribs" />
</xsl:template>

<xsl:attribute-set name="attribs">
  <xsl:attribute name="{local-name()}"><xsl:value-of select="." /></xsl:attribute>

   <!-- THIS BIT WORKS WHEN UNCOMMENTED! 
   create three attributes, assign the value of the <first>, <middle>, or <last> elements to the attribute 
   <xsl:attribute name="first"><xsl:value-of select="first" /></xsl:attribute>
   <xsl:attribute name="middle"><xsl:value-of select="middle" /></xsl:attribute>
   <xsl:attribute name="last"><xsl:value-of select="last" /></xsl:attribute> 
   -->
</xsl:attribute-set>

</xsl:stylesheet>
I can get it to work when I explicitly refer to the element names in the attribute-set, but not if I wish to be a bit more dynamic and use the local-name() function. Can someone please explain where I am going wrong here?

Many Thanks
__________________
Torrent
www.ski-info-online.com

...silently carving the soft deep powder...

Reply With Quote
  #2  
Old June 2nd, 2003, 01:20 PM
torrent torrent is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Location: UK
Posts: 59 torrent User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Thanks echolalia.

Yeah, I did get it working with a for-each in the end but just wondered if it could be done the other (with just apply-templates). Anyhoo, I've just gone an ordered Oreilly's XSLT book

Thanks again for the response.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > XSLT: can't get {local-name()} to work


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway