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 14th, 2003, 12:38 PM
krayziekup krayziekup is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Bosie, ID
Posts: 7 krayziekup User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
XSL question

I have an XML with the following structure:

<styleguide>
<rule>
<name>style guide rule</name>
<description>Some content <link href=”some url”>content that is a hyperlink </link> more content</description>
</rule>

<rule>
<name>another style guide rule</name>
<description>Content <link href="some url">content that is a hyperlink </link> More content </description>
</rule>
</styleguide>

Not all description tags have content that are links but some do. Description tags may have 0 link tags or multiple link tags in them. My question is how do I display all the content within a description tag and have the link elements (if any exist) be displayed as hyperlinks? I can figure out how to display a description tag with one link element using xpath functions substring-before and substring-after but if there is more than one link element in the description tag I get stuck. My last question is, can variables be used in XSL like a standard programming language? If so I might be able to figure a solution with the use of variables. Any help on this would be gratefully appreciated.

Reply With Quote
  #2  
Old August 14th, 2003, 10:05 PM
pramenjatchek pramenjatchek is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 6 pramenjatchek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
You can give each link in your xml a unique id, eg

<styleguide>
<rule>
<name>style guide rule</name>
<description>Some content <link id="1">content that is a hyperlink </link> more content</description>
</rule>

<rule>
<name>another style guide rule</name>
<description>Content <link id="2">content that is a hyperlink </link> More content </description>
</rule>
</styleguide>

then add the following at the end of your file:

<linkResources>
<link id="1">
<linkURL>http://www.urlhere.com</linkURL>
</link>
<link id="2">
<linkURL>http://www.anotherurl.com</linkURL>
</link>

In the xsl file, you can then do the following:

<xsl:template match="description">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="link | description/link">
<xsl:variable name="linkId">
<xsl:value-of select="@id"/>
</xsl:variable>

<xsl:variable name="linkURL">
<xsl:value-of select="/*/linkResources/link[@id=$linkId]/linkURL"/>
</xsl:variable>

<a href="{$linkURL}"><xsl:apply-templates/></a>
</template>

Where it says /*/ in the linkURL variable you may have to amend to what the specific path in your document is, this is just an example path. The two IDs are basically compared and if they match the value of linkURL is stored in the variable.

This method will pick up all the text and links etc in the description tag. Also, you can keep all your page resources like links in a central place in your document.

Hope this helps

Reply With Quote
  #3  
Old August 15th, 2003, 12:53 PM
krayziekup krayziekup is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Bosie, ID
Posts: 7 krayziekup User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks a ton for the reply. I applyed the style sheet and with a little bit of tweaking got it to work.

Reply With Quote
  #4  
Old August 15th, 2003, 05:45 PM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
If that way works, great, but here's a way that's a bit less complicated and lets your retain your earlier (simpler) xml structure.

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

	<xsl:output method="html"/>
	
	<xsl:template match="/">
		<html>
			<body>
			<xsl:apply-templates />
			</body>
		</html>
	</xsl:template>
	
	<xsl:template match="//rule">
		<p>
			<xsl:apply-templates />
		</p>
	</xsl:template>
	
	<xsl:template match="//name">
		<b><xsl:apply-templates /></b><br/>
	</xsl:template>
	
	<xsl:template match="//description">
		<xsl:apply-templates />
	</xsl:template>
	
	<xsl:template match="//link">
		<a href="{@href}"><xsl:apply-templates /></a>
	</xsl:template>
	
	<xsl:template match="text()">
		<xsl:value-of select="."/>
	</xsl:template>

</xsl:stylesheet>

Reply With Quote
  #5  
Old August 18th, 2003, 02:21 PM
krayziekup krayziekup is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Bosie, ID
Posts: 7 krayziekup User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for the help. I applied the second style sheet and it works great and keeps things simple. Makes my boss happy.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > XSL question


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