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:
  #1  
Old August 18th, 2004, 01:26 PM
thundza thundza is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 5 thundza User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 7 sec
Reputation Power: 0
Using XSLT to set attribute value.

Hello everybody,

Let me present the problem. Let's say I have an XML file like this:

<root>
<link source="www.google.com">Google</link>
</root>

And I want to tranform it into this using XSLT:

<a href="www.google.com">Google</a>

This is what I have so far for an XSLT:

<xsl:template match="/">
<a href = " [unknown] ">
<xsl:value-of select="root/link" />
</a>
</xsl:template>

I need to figure out how to put the source attribute from the link element into the href attribute (the unknown). Any help would be appreciated.

Reply With Quote
  #2  
Old August 18th, 2004, 01:53 PM
rguilbault rguilbault is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 30 rguilbault User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m 18 sec
Reputation Power: 5
Quote:
Originally Posted by thundza
Hello everybody,

Let me present the problem. Let's say I have an XML file like this:

<root>
<link source="www.google.com">Google</link>
</root>

And I want to tranform it into this using XSLT:

<a href="www.google.com">Google</a>

This is what I have so far for an XSLT:

<xsl:template match="/">
<a href = " [unknown] ">
<xsl:value-of select="root/link" />
</a>
</xsl:template>

I need to figure out how to put the source attribute from the link element into the href attribute (the unknown). Any help would be appreciated.

Hello. Try:

<xsl:template match="/">
...
<xsl:apply-templates select="/root/link" />
...
</xsl:template

<xsl:template match="link">
<a href="{@source}"><xsl:value-of select="." />
</xsl:template>

You can apply the link template individually or you can apply ALL defined templates by simply stating: <xsl:apply-templates /> -- be careful with this, though, as it can lead to duplicate information if you reference other templates individually.

Secondly, in order to specify an Xpath expression to assign an attribute to an HTML element, you need to escape it with {} within the quotes, hence href="{@source}". You can specify any XPath expression (to my knowledge) within the curly braces -- so something like href="{link/external/href}" could be used if you had an xml structure like:

<link>
<external>
<text>Text</text>
<href>www.google.com</href>
</extrenal>
</link>

Hope that helps.

Reply With Quote
  #3  
Old August 18th, 2004, 04:24 PM
NotGoddess's Avatar
NotGoddess NotGoddess is offline
Kung-fu Kitty
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 350 NotGoddess User rank is Sergeant (500 - 2000 Reputation Level)NotGoddess User rank is Sergeant (500 - 2000 Reputation Level)NotGoddess User rank is Sergeant (500 - 2000 Reputation Level)NotGoddess User rank is Sergeant (500 - 2000 Reputation Level)NotGoddess User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 6 h 9 m 2 sec
Reputation Power: 10
Send a message via AIM to NotGoddess
Here's another way to skin that cat as well:

PHP Code:
<root>
<
link source="www.google.com">Google</link>
</
root


PHP Code:
<xsl:for-each select="/root/link">
  <
a>
    <
xsl:attribute name="href"><xsl:value-of select="@source"/></xsl:attribute>
    <
xsl:value-of select="."/>
  </
a>
</
xsl:for-each

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > Using XSLT to set attribute value.


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 6 hosted by Hostway
Stay green...Green IT