|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
||||
|
||||
|
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> Code:
<xsl:value-of select="" disable-output-escaping="yes" />
__________________
# Jeremy Explain your problem instead of asking how to do what you decided was the solution. |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
||||
|
||||
|
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> |
|
#6
|
|||
|
|||
|
I meant that putting markup inside CDATA isn't a great idea!
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > tranform text inside a <xsl:value-of> |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|