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 October 30th, 2003, 09:59 AM
imbrokn imbrokn is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: NJ
Posts: 428 imbrokn User rank is Corporal (100 - 500 Reputation Level)imbrokn User rank is Corporal (100 - 500 Reputation Level)imbrokn User rank is Corporal (100 - 500 Reputation Level)imbrokn User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 11 h 34 m 8 sec
Reputation Power: 10
Send a message via AIM to imbrokn
tranform text inside a <xsl:value-of>

let's say i have an XML document like this.

Code:
<body> 
   <head>Blah</head> 
   This is where para data goes. 
   <head>Here starts another section</head> 
   And this could be <bold>ALOT</bold> of copy. 
</body>


Now my problem is i want to display the entire contents of <body> but i want to format all data within the head elements as say <span class="heading"> and convert all <bold>'s to <b> I've tried making a template for head, but then when I call apply-templates, it will just list all the head elements not, keeping the data where it should be. How can i go about formating this content? Thanks.

Reply With Quote
  #2  
Old November 4th, 2003, 05:05 PM
teedee teedee is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 42 teedee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
hehe, I struggled with this one! The problem is in the way you've done the XML, the hierarchy is all wrong. You've got <bold> as a child of <body> and there's no easy way to determine which text node is associated with a <head> element .... here's how I would have done it

Code:
<body>
	<section>
		<head>Blah</head>
		<sectionText>
			<notBold>This is where para data goes.</notBold>
		</sectionText>
	</section>
	<section>
		<head>Here starts another section</head>
		<sectionText>
			<notBold>And this could be </notBold>
			<bold>A LOT</bold>
			<notBold> of copy.</notBold>
			<otherStuff>other stuff!</otherStuff>
			<notBold>This text </notBold>
			<bold>IS BOLD!!!!</bold>
		</sectionText>
	</section>
</body>


That makes the XSL a breeze. The only tricky part is using child::* and checking for the name() of each node like this

Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="/">
		<html>
			<head/>
			<body>
				<xsl:apply-templates select="/body/section"/>
			</body>
		</html>
	</xsl:template>
	<xsl:template match="/body/section">
		<span class="heading" style="color:red">
			<xsl:value-of select="head"/>
		</span>
		<xsl:apply-templates select="sectionText"/>
	</xsl:template>
	<xsl:template match="sectionText">
		<p>
			<!-- This loops through all the bold and notBold elements -->
			<xsl:for-each select="child::*">
				<xsl:choose>
					<!-- name() returns the name of the element -->
					<xsl:when test="name()='notBold'">
						<xsl:value-of select="."/>
					</xsl:when>
					<xsl:when test="name()='bold'">
						<b>
							<xsl:value-of select="."/>
						</b>
					</xsl:when>
					<!-- xsl:otherwise will catch anything not matched in the xsl:when statements -->
					<xsl:otherwise>
						<pre>*** I found a <xsl:value-of select="name()"/> element and I don't know what to do!!! 

Please write more XSL here, I'm confused!!!***</pre>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>
		</p>
	</xsl:template>
</xsl:stylesheet>


The moral of the story is - use more elements! And think about which elements are children of other elements.

Reply With Quote
  #3  
Old November 4th, 2003, 07:39 PM
jharnois's Avatar
jharnois jharnois is offline
mod_dev_shed
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Sep 2002
Location: Atlanta, GA
Posts: 14,299 jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 Days 1 h 54 m 17 sec
Reputation Power: 760
XML is used to describe data. Placing text inside a <bold> element doesn't describe what the data is, it describes how the data should look. This is incorrect.

Working with teedee's XML:
Code:
<body>
	<section>
		<head>Blah</head>
		<sectionText><![CDATA[This is where para
 data goes.]]></sectionText>
	</section>
	<section>
		<head>Here starts another section</head>
		<sectionText><![CDATA[And this could be <span class="important">A LOT</span> 
of text, including HTML tags.]]></sectionText>
	</section>
</body>
Using CDATA allows you to put HTML into your text. You want to use HTML that has no meaning unless you apply some CSS to it, so you wouldn't want to use <b>, but <span>s instead. This allows you to change the appearance of the XML output w/o changing the XML itself. To keep XSL from making < into &lt;, you have to
Code:
<xsl:value-of select="" disable-output-escaping="yes" />
I believe this is the proper way to handle this situation.
__________________
# Jeremy

Explain your problem instead of asking how to do what you decided was the solution.

Reply With Quote
  #4  
Old November 5th, 2003, 01:27 PM
teedee teedee is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 42 teedee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Well allow me to retort!
Samuel L Jackson, Pulp Fiction

That's a valid point but it negates one of the main advantages of XML - keeping content and presentation separate!

Putting html markup inside CDATA means you can no longer reference the <span> elements using XSL so you wouldn't be able to transform them, they would just be markup no matter what XSL you used. The only real use for CDATA is in getting XSL to output Javascript with > and < or VBScript using &'s

Last edited by teedee : November 5th, 2003 at 01:29 PM.

Reply With Quote
  #5  
Old November 5th, 2003, 05:44 PM
jharnois's Avatar
jharnois jharnois is offline
mod_dev_shed
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Sep 2002
Location: Atlanta, GA
Posts: 14,299 jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 Days 1 h 54 m 17 sec
Reputation Power: 760
How is wrapping text in a <bold> tag separating content from presentation. As I said before, that doens't describe the data, it describes how the data should look.

There is no need to access the data in <bold> any differently than the rest of the data in sectionText with XSL, only when modifying how it will look, which can be accomplished with CSS.
Code:
/* stylesheet.css */
p
  {
  font-size: small;
  }
span.header
  {
  font-weight: bold;
  font-size: medium;
  }
span.important
  {
  font-wieght: bold;
  }
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title>Articles</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css" />
</head>
<body>
<xsl:for-each select="body/section">
<p><span class="header"><xsl:value-of select="head" /></span><br />
<xsl:value-of select="sectionText" disable-output-escaping="yes" /></p>
</xsl:for-each>
</body>
</html>
</xsl:template>

Reply With Quote
  #6  
Old November 6th, 2003, 04:16 AM
teedee teedee is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 42 teedee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
I meant that putting markup inside CDATA isn't a great idea!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > tranform text inside a <xsl:value-of>


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