Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,815 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,162 people online right now. Registration is fast and FREE... Join Now!




XSLT transforms

 
Reply to this topicStart new topic

XSLT transforms, Why are they so picky?

indrora
26 Jul, 2008 - 07:23 PM
Post #1

D.I.C Head
Group Icon

Joined: 25 Jul, 2008
Posts: 52


Dream Kudos: 25
My Contributions
So i'm working on an XSLT transform to handle resumes... kind of an on-the-job project because i need a resume.
So i've got this:
xml


<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="html" version="4.0"
encoding="iso-8859-1" indent="yes"/>
<xsl:template match="/">

<html>

<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
<meta name="description" content="the resume for Morgan Gangwere"/>
<meta name="keywords" content="keywords"/>
<meta name="author" content="author"/>
<link rel="stylesheet" type="text/css" href="default.css" media="screen"/>
<title>resume for <xsl:value-of select="/resume/person/name" /></title>
</head>

<body>

<div class="container" >

<div class="main" >

<div class="header">

<div class="title">
<h1>resume</h1>

</div>
</div>

<div class="content" >
<xsl:for-each select="/resume/sections/section">
<div class="item">
<h1><xsl:value-of select="@name" /></h1>
<xsl:copy-of select="." />
<xsl:apply-templates />
</div>
</xsl:for-each>
</div>

<div class="sidenav">
<xsl:apply-templates select="/resume/person" />

<h1>about</h1>
<xsl:value-of select="/resume/person/about" />

</div>

<div class="clearer"><span> </span></div>
<div style="height:100%"> </div>
</div>

<div class="footer">© 2008 <xsl:value-of select="/resume/person/name" />. Valid <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a> & <a href="http://validator.w3.org/check?uri=referer">XHTML</a>. Template design by <a href="http://templates.arcsin.se">Arcsin</a>
</div>

</div>

</body>
</html>
</xsl:template>

<xsl:template match="person">

<center> <h1><xsl:value-of select="name" /></h1>
<img src="{@picture}" /></center>

<ul>
<li>Email: <xsl:value-of select="email" /></li>
<li>Title: <xsl:value-of select="profession" /></li>
<li>Birthday: <xsl:value-of select="birthdate" /></li>
<xsl:for-each select="phone">
<li>Phone (<xsl:value-of select="@kind" />): <xsl:value-of select="." /> </li>
</xsl:for-each>
<li>Address: <address><xsl:value-of select="address" /></address></li>
</ul>

</xsl:template>
</xsl:stylesheet>


and this to feed to it:
xml

<?xml version="1.0" encoding="UTF-8" ?>


<?xml-stylesheet href="style.xml" type="text/xsl" ?>
<resume>

<person picture="itsjoe.jpg">
<name> Joe anybody </name>
<profession>Programmer</profession>
<email>spam@camalot.com</email>
<address>123 Anywhere</address>
<birthdate>6/6/04</birthdate>
<phone kind="cell">1 555 542 3234</phone>
<about>
...
</about>

</person>
<sections>
<section name="Mission">...
</section>
<section name="Interests">
...
</section>

<section name="Qualifications">
...
</section>
<section name="Experience">
...
</section>
<section name="Professional Interactions">
...
</section>

<section name="Previous work">

...
</section>

</sections>
</resume>


this transforms and displays in Firefox, Dies in IE and on both i cant get ANY sub-tags (such as <p> or <img>) to be parsed.
why
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: XSLT Transforms
26 Jul, 2008 - 07:42 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,227



Thanked: 218 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well remember that <p> and <img> tags are considered child elements of their containing elements. So you can access them through their parent element or easier way is to give them ids or names attributes and access them that way. I typically pluck them out by name, set them to a variable and then can use the variable in the rest of the xsl template definition.

Or if you just want to render all in a given section you can use a template definition like...

CODE

<xsl:template match="p">
        <p><xsl:value-of select="." /></p>
</xsl:template>


This will match all <p> elements and put their value (the text in between the<p> tags) and manually puts them in <p> tags for display.

Just some tips for you.

smile.gif
User is online!Profile CardPM
+Quote Post

indrora
RE: XSLT Transforms
26 Jul, 2008 - 07:55 PM
Post #3

D.I.C Head
Group Icon

Joined: 25 Jul, 2008
Posts: 52


Dream Kudos: 25
My Contributions

Well i dont know what kind of tags are going to be shoved into a <section> tag. I may just do this:
xml

<xsl:template match="section/*">
<<xsl:value-of select="name()" />> <xsl:value-of select="." /></<xsl:value-of select="name()">
</xsl:template>


This post has been edited by indrora: 26 Jul, 2008 - 07:56 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: XSLT Transforms
26 Jul, 2008 - 08:14 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,227



Thanked: 218 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
All the more reason to know a bit more about what will be in the XML, enough to create a DTD, then code your XSL according to the DTD. That is the ideal situation but whatever works for you. smile.gif
User is online!Profile CardPM
+Quote Post

indrora
RE: XSLT Transforms
26 Jul, 2008 - 08:19 PM
Post #5

D.I.C Head
Group Icon

Joined: 25 Jul, 2008
Posts: 52


Dream Kudos: 25
My Contributions
I'll write a DTD when i get the chance to. At the moment i'm trying to just not mess up my mind.

Oh and one other thing, i've tried including the XHTML dtd into the document and it still doesnt work. Oh well...
User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: XSLT Transforms
31 Jul, 2008 - 02:05 PM
Post #6

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
The easiest way to deal with this is to add a default rule.

Example:

xml

<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="* | text()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="text()">
<xsl:copy-of select="."/>
</xsl:template>


The default rule should have a priority lower than the rules that match specific elements, so that it will only be invoked if no other rules fit. Rule priorities can be manually adjusted, by the way.

It might be even better to have the resume tags in a separate namespace for error checking purposes, as you can then have:

xml

<xsl:template match="ns:*">
<xsl:message terminate="yes">Unexpected element in the ns namespace: <xsl:value-of select="name()"/>.
</xsl:message>
</xsl:template>


This post has been edited by perfectly.insane: 31 Jul, 2008 - 02:06 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 02:44PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month