Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts

    Learning how to merge XSL and XML

    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>

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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"
    Last edited by brothercake; 02-19-2003 at 10:15 PM.

  • #3
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Newcastle, England
    Posts
    178
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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">
    <xslutput 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" />
    &#160;
    <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>

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Newcastle, England
    Posts
    178
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by mpjbrennan
    but I'm not a politician.
    I was just making sure, in case I've misunderstood the syntax all along ...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •