PDA

View Full Version : Learning how to merge XSL and XML


arnyinc
02-19-2003, 09:47 PM
I have a basic XML sheet setup for a contact list

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="simple.xsl" ?>
<contactlist>
<contact>
<firstname>joe</firstname>
<lastname>smith</lastname>
<email>joesmith@company.com</email>
<phone>555-555-5555</phone>
</contact>

<contact>
<firstname>bill</firstname>
<lastname>williams</lastname>
<email>billwilliams@company.com</email>
<phone>111-111-1111</phone>
</contact>
</contactlist>

Now I am trying to apply an XSL style sheet and I have a couple very basic questions. First, I cannot get a space to appear between the firstname and lastname. They are squished together when I view it. Second, the way I have the email link setup causes a massive error and makes it break. How would I integrate the email and name to just get something like <a href="mailto:billwilliams@company.com">Bill Williams</a>


<?xml version="1.0" encoding="ISO-8859-1"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/TR/xhtml1/strict">
<body bgcolor="#cccccc">
<xsl:for-each select="contactlist/contact">
<div style="background-color:teal;color:white;padding:4px">
<a href="mailto:<xsl:value-of select="email"/>"><xsl:value-of select="firstname"/> <xsl:value-of select="lastname"/></a>
</div>
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<xsl:value-of select="phone"/>
</div>
</xsl:for-each>
</body>
</html>

brothercake
02-19-2003, 10:07 PM
You're beginning and ending your stylesheet with <html> - and so it's not a stylesheet, it's an XHTML document with XSL inside it. Or at any rate ...
it might be one of those content-negotiation things, that Mozilla implicitly understands but IE chokes over.

Either way - you have no <xsl:stylesheet/> element - which has to be the root element of an XSL stylesheet, so I wouldn't be surprised if your other problems are springing from that.

Try the XSL tutorial at w3schools for more info about stylesheet formatting http://www.w3schools.com/xsl/default.asp

btw - there's no such mimetype as "text/xsl" - it should be "text/xml"

arnyinc
02-20-2003, 01:04 PM
Thanks for the info. I was going by the samples listed at http://www.w3schools.com/xml/xml_examples.asp under "XML and XSL" and they don't appear to be entirely valid. I'll go for their XSL section and see if i can figure it out from there.

mpjbrennan
02-20-2003, 04:24 PM
You have to use <xsl:attribute> to create a link. Here's an example stylesheet which will transform your XML

patrick

-----------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="/">
<html style="background:#cccccc">
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>

<xsl:template match="contact">
<div style="background:teal;padding:4px; width:100px">
<a style="color:#ffffc6">
<xsl:attribute name="href">mailTo:<xsl:value-of select="email" /></xsl:attribute>
<xsl:value-of select="firstname" />
 
<xsl:value-of select="lastname" />
</a>
</div>
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt; width:100px">
<xsl:value-of select="phone"/>
</div>
</xsl:template>

</xsl:stylesheet>

brothercake
02-20-2003, 04:59 PM
Originally posted by mpjbrennan
You have to use <xsl:attribute> to create a link.

I don't think you *have* to - wouldn't the shorthand method be equally effective:

<a href="mailto:{email}">mailto link</a>

mpjbrennan
02-20-2003, 06:34 PM
I guess it would have been more accurate to say:

"You need to set the attribute using <xsl:attribute> for example" - but I'm not a politician.

Thanks for pointing out the alternative.

patrick

brothercake
02-20-2003, 06:40 PM
Originally posted by mpjbrennan
but I'm not a politician.

:D I was just making sure, in case I've misunderstood the syntax all along ...