473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Applying templates

I'm still learning how to write stylesheets and the ones I've come up with
for learning purposes are pretty simple and straightforward. I can get it
to work, but probably not in the most ideal way.

I have a node in the XML I'm transforming that contains CRLFs. I did a
search on how to do a search/replace but am not 100% sure how to fit it into
my template. Here is a sample xml record:

<UserRecordRoot>
<UserItems>
<UserItem>
<uid>MyUserId</uid>
<lastLogin>2002-02-21 01:04:36</lastLogin>
<country>USA</country>
<comments>
Comments Line 1
Comments Line 2
Comments Line 3
</comments>
</UserItem>
</UserItems>
</UserRecordRoot>

Here is my XSL

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

<xsl:output method="html"/>

<xsl:template match="UserItems">
<html>
<head>
<title>Game Sets</title>
</head>
<body>
<h1>Users</h1>
<table border="1" width="100%">
<tr>
<th>User</th>
<th>Last Login</th>
<th>Country</th>
<th>Comments</th>
</tr>
<xsl:for-each select="UserItem">
<tr>
<td valign="top">
<xsl:copy-of select="name"/>
</td>
<td valign="top">
<xsl:copy-of select="lastLogin"/>
</td>
<td valign="top">
<xsl:copy-of select="country"/>
</td>
<td valign="top" width="25%">
<xsl:value-of select="comments"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="comments">
<xsl:call-template name="replace">
<xsl:with-param name="string" select="."/>
</xsl:call-template>
</xsl:template>

// I found the below on one of the sites listed in my google search
<xsl:template name="replace">
<xsl:param name="string"/>
<xsl:choose>
<xsl:when test="contains($string,' ')">
<xsl:value-of select="substring-before($string,' ')"/>
<br/>
<xsl:call-template name="replace">
<xsl:with-param name="string"
select="substring-after($string,' ')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

The UserItems template is simpe, straightfoward and works. I'm just not
sure how to make it so that the comments node matches the template of the
same name and thus triggering the replace template. How can I get this
done?

thnx,
Christoph
Sep 25 '06 #1
3 1379
Christoph wrote:
The UserItems template is simpe, straightfoward and works. I'm just not
sure how to make it so that the comments node matches the template of the
same name and thus triggering the replace template. How can I get this
done?
Replace
<xsl:value-of select="comments"/>
with
<xsl:apply-templates select="comments"/>

I would suggest doing the same thing with the UserItem, actually --
replace the for-each with
<xsl:apply-templates select="UserItem"/>

and move the logic into a template:

<xsl:template match="UserItem">
<xsl:for-each select="UserItem">
<tr>
<td valign="top">
<xsl:copy-of select="name"/>
</td>
<td valign="top">
<xsl:copy-of select="lastLogin"/>
</td>
<td valign="top">
<xsl:copy-of select="country"/>
</td>
<td valign="top" width="25%">
<xsl:apply-templates select="comments"/>
</td>
</tr>
</xsl:for-each>
</xsl:template

Generally this approach -- separately say "process these" and "here's
how to process them" and let XSLT tie the two together -- is more
flexible. It doesn't make much difference in this case since you're
explicitly saying what nodes to continue processing with, but it will
matter when you want to say things like "process all the children".

You can write procedural XSLT... but it's better to think of it as
declarative.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Sep 26 '06 #2
(Whups. Typo in first version of this note; corrected here.)

Christoph wrote:
The UserItems template is simpe, straightfoward and works. I'm just not
sure how to make it so that the comments node matches the template of the
same name and thus triggering the replace template. How can I get this
done?
Replace
<xsl:value-of select="comments"/>
with
<xsl:apply-templates select="comments"/>

I would suggest doing the same thing with the UserItem, actually --
replace the for-each with
<xsl:apply-templates select="UserItem"/>

and move the logic into a template:

<xsl:template match="UserItem">
<tr>
<td valign="top">
<xsl:copy-of select="name"/>
</td>
<td valign="top">
<xsl:copy-of select="lastLogin"/>
</td>
<td valign="top">
<xsl:copy-of select="country"/>
</td>
<td valign="top" width="25%">
<xsl:apply-templates select="comments"/>
</td>
</tr>
</xsl:template

Generally this approach -- separately say "process these" and "here's
how to process them" and let XSLT tie the two together -- is more
flexible. It doesn't make much difference in this case since you're
explicitly saying what nodes to continue processing with, but it will
matter when you want to say things like "process all the children".

You can write procedural XSLT... but it's better to think of it as
declarative.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Sep 26 '06 #3
(Whups. Typo in first version of this note; corrected here.)

Christoph wrote:
The UserItems template is simpe, straightfoward and works. I'm just not
sure how to make it so that the comments node matches the template of the
same name and thus triggering the replace template. How can I get this
done?
Replace
<xsl:value-of select="comments"/>
with
<xsl:apply-templates select="comments"/>

I would suggest doing the same thing with the UserItem, actually --
replace the for-each with
<xsl:apply-templates select="UserItem"/>

and move the logic into a template:

<xsl:template match="UserItem">
<tr>
<td valign="top">
<xsl:copy-of select="name"/>
</td>
<td valign="top">
<xsl:copy-of select="lastLogin"/>
</td>
<td valign="top">
<xsl:copy-of select="country"/>
</td>
<td valign="top" width="25%">
<xsl:apply-templates select="comments"/>
</td>
</tr>
</xsl:template>

Generally this approach -- separately say "process these" and "here's
how to process them" and let XSLT tie the two together -- is more
flexible. It doesn't make much difference in this case since you're
explicitly saying what nodes to continue processing with, but it will
matter when you want to say things like "process all the children".

You can write procedural XSLT... but it's better to think of it as
declarative.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Sep 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kimmo | last post by:
Folks, I have a XML document that has been put together from a "dynamic part" (generated somehow during runtime) and a "static part" (read from a control file). Basically the document looks like...
2
by: Thomas Sommer | last post by:
Hi, I think the following is not possible but maybe (hopefully) I am wrong: I have: <xsl:param name="test"> <tag1>asdfasdf</tag1> <tag2>asdfasdf</tag2> </xsl:param>
6
by: Tim Meagher | last post by:
Can anyone help me figure out how to apply a stylesheet to a pushbutton defined in the asp:BoundColumn or asp:EditCommandColumn elements of a datagrid?
28
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
1
by: Ben R. | last post by:
Hi, I'm writing a .NET winforms app that serves as an email client. Users can store message templates. This is done via an XML document for loading and saving the templates. When the template is...
6
by: Orgun | last post by:
Hi, I sent this message to the moderated c++ group too but it is waiting for moderator approval and I wanted to send here too. I am new to Design Patterns. I want to write a simple...
3
by: tuka | last post by:
Hi, I have a problem I would like help solving: My xml: <node id=1> <nodeContent>xyz</nodeContent> </node> <node id=2>
6
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
Hi - I have 4 webservers in my webfarm. All Win2k3 web edition. Before yesterday, none of them were service packed. I have now applied SP2 to two of them, and I'm getting a very weird MSDTC...
2
by: Hvid Hat | last post by:
Hi When using <xsl:apply-templates select="Document"I get 10 documents. How can I limit it to only the first 5 documents, e.g. pseduo <xsl:apply-templates select="Document">
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.