473,406 Members | 2,867 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,406 software developers and data experts.

Help with Basic XML example

5
I am trying to learn XML and interacting w/ C# and ASP.NET forms. I am not getting the output I expect, and wanted to pose that question of "why" to the group here.

My XSLT:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes" method="html"/>
<xsl:template match="/">
<html>
<head>
<style type="text/css">
.redText {font-family:arial;color:#ff0000;}
.borders {border-left:1px solid #0000ff;
border-right:1px solid #00ff00;
border-top:1px solid #00ff00;
border-bottom:1px solid #0000ff;}
</style>
</head>
<body bgcolor="#000000">
<table class="borders" border="0" width="800" cellpadding="4"
cellspacing="0" bgcolor="#efefef">
<xsl:apply-templates select="menuBar"/>
<tr height="100%">
<td>News Goes Here<br/>
<xsl:apply-templates select="News"/>
</td>
<td class="redText"><xsl:apply-templates select="MainSection"/></td>
</tr>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="News">
<table class="borders" border="0" width="640" cellpadding="4"
cellspacing="0" bgcolor="#ff88cc">
<tr><td></td></tr>
</table>
</xsl:template>
<xsl:template match="MainSection">
<span>Hello World!</span>
<p/>
</xsl:template>
<xsl:template match="menuBar">
<tr>
<td>
<table class="borders" border="0" width="640" cellpadding="4"
cellspacing="0" bgcolor="#ff88cc">
<tr>
<td>register</td>
<td>menu item 1</td>
</tr>
</table>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>




My C# PageLoad Method:

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
datasets.UserInfoDS ds = new datasets.UserInfoDS();
XmlDataDocument dd = new XmlDataDocument(ds);

string xslPath = Server.MapPath("CommonTemplate.xslt");
StringWriter sw = new StringWriter();
XslTransform xsl = new XslTransform();
xsl.Load(xslPath);


//Instantiate the XPathDocument Class
//XPathDocument doc = new XPathDocument(xmlPath);


//Instantiate the XslTransform Class
xsl.Transform(dd,null,sw,null);

Response.Write(sw.ToString());
}




But I'm getting the following as output:

<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-16">
<style type="text/css">
.redText {font-family:arial;color:#ff0000;}
.borders {border-left:1px solid #0000ff;
border-right:1px solid #00ff00;
border-top:1px solid #00ff00;
border-bottom:1px solid #0000ff;}
</style>
</head>
<body bgcolor="#000000">
<table class="borders" border="0" width="800" cellpadding="4" cellspacing="0" bgcolor="#efefef">
<tr height="100%">
<td>News Goes Here<br></td>
<td class="redText">
</td>
</tr>
</table>
</body>
</html>

I am expecting to at least see menubar words, the table under the News area, and the words Hello World in the MainSection. None of those show up. Any ideas? The dataset is just a blank dataset.

Thanks!
Sep 24 '07 #1
8 1668
jkmyoung
2,057 Expert 2GB
My guess is:
<xsl:apply-templates select="News"/>
is not properly matching up.

I'm guessing the News node is at the wrong level.
You may want to try xpath "//News" or "*/News"

if it's a namespace problem (xmlns="..."), try "//*[local-name()='News']"
Sep 24 '07 #2
delcov
5
Since I am using a blank dataset, will that affect the fact that the menuBar, News and MainSection templates are not being applied? But my assumption was that since I'm calling "apply-template", it would attempt to apply the template regardless of the data pushed to the page. By that I mean, if I apply the News template, and the news template has an encompassing table around dynamically generated "NewsItem" Rows, if you have no NewsItem rows, you would at least see the encompassing table.
Sep 25 '07 #3
jkmyoung
2,057 Expert 2GB
The way you've called the template is node driven, matching only News nodes.

You'd want to use call-template with named templates instead. eg:
<xsl:call-template name="News"/>

And then have your template:
<xsl:template name="News">


http://www.w3schools.com/xsl/xsl_w3celementref.asp
Sep 25 '07 #4
delcov
5
Thanks for your reply. I think I'm closer to my answer. Now, I get the error message 'xsl:call-templates' cannot be a child of 'td' element when I run my web app. I'm assuming that I'm using call-templates incorrectly, that it's not meant to just pull a template into the spot where it's called. That was my intent in the xslt file. Basically, i wanted to see a webpage where the menu bar goes across the top of the page, and underneath that are 2 sections, a news section and the Main section, where each section will eventually be dynamic. Right now, the dataset hasn't been built to pass in menuBar objects or News objects.
Sep 25 '07 #5
delcov
5
Ok. I've done some reading, thought about it, and modified my code in this way. I'm still not getting the results I'm expecting. I'm providing the info below:


XSLT:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes" method="html"/>
<xsl:template match="/">
<html>
<head>
<style type="text/css">
.redText {font-family:arial;color:#ff0000;}
.borders {border-left:1px solid #0000ff;
border-right:1px solid #00ff00;
border-top:1px solid #00ff00;
border-bottom:1px solid #0000ff;}
</style>
</head>
<body bgcolor="#00ff00">
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template name="Page">
<table class="borders" border="0" width="800" cellpadding="4" cellspacing="0" bgcolor="#efefef">
<tr>
<xsl:apply-templates select="menuBar"/>
</tr>
<tr height="100%">
<td>News Goes Here<br/>
<xsl:apply-templates select="NewsItem"/>
</td>
<td class="redText"><xsl:apply-templates select="MainForm"/></td>
</tr>
</table>
</xsl:template>
<xsl:template name="NewsItem">
<table class="borders" border="0" width="640" cellpadding="4"
cellspacing="0" bgcolor="#ff88cc">
<tr><td></td></tr>
</table>
</xsl:template>
<xsl:template name="MainForm">
<span>Hello World!</span>
<p/>
</xsl:template>
<xsl:template name="menuBar">
<td>register</td>
<td>menu item 1</td>
</xsl:template>
</xsl:stylesheet>





The XML (generated from an ADO.NET dataset):

<?xml version="1.0" standalone="yes"?>
<Page xmlns="http://tempuri.org/Page.xsd">
<menuBar>
<name>register</name>
<URL>http://register.com</URL>
</menuBar>
<MainForm>
<title>Home.aspx</title>
</MainForm>
<NewsItem>
<description>Description of news item</description>
<story>The story of the item goes here.</story>
</NewsItem>
</Page>



The results i get are:

<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-16">
<style type="text/css">
.redText {font-family:arial;color:#ff0000;}
.borders {border-left:1px solid #0000ff;
border-right:1px solid #00ff00;
border-top:1px solid #00ff00;
border-bottom:1px solid #0000ff;}
</style>
</head>
<body bgcolor="#00ff00">registerhttp://register.comHome.aspxDescription of news itemThe story of the item goes here.</body>
</html>


It looks like it's just spitting the data from my dataset out at the location in the XSLT where I call <xsl:apply-templates/>. Can anyone help me to figure this out??? Thanks!
Sep 26 '07 #6
jkmyoung
2,057 Expert 2GB
call-template not
call-templates

Change:
<xsl:apply-templates/>
to
<xsl:call-template name="Page"/>

<xsl:apply-templates select="menuBar"/>
to
<xsl:call-templates name="menuBar"/>


I'm assuming NewsItem is your repeating node in your dataset.
Sep 27 '07 #7
delcov
5
Your assumption is correct. And the code now works. Thanks for your help and patience. I'm slowly learning how to manipulate XML, and need to read the specs I guess. Thanks again.
Sep 27 '07 #8
XML Studio (which is free) contains an XPath expression builder, which also provides sample code showing how to use the expression in XSLT, java, C# etc.
This is particularly handy with complex expressions or namespaces...

http://www.liquid-technologies.com

My guess is:
<xsl:apply-templates select="News"/>
is not properly matching up.

I'm guessing the News node is at the wrong level.
You may want to try xpath "//News" or "*/News"

if it's a namespace problem (xmlns="..."), try "//*[local-name()='News']"
Oct 2 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
4
by: Terencetrent | last post by:
I having been using Access '97/2002 for about 4 years now and have never really had the need or the time to learn visual basic. Well, I think the time has finally come. I need help with Visual...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
12
by: =?Utf-8?B?ZGdvdw==?= | last post by:
I designed a "contact_us" page in visual web developer 2005 express along with EW2 after viewing tutorials on asp.net's help page. Features work like they should, but I cannot figure out how to...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.