SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 53

Thread: Xsl

  1. #1
    SitePoint Zealot ohnnyj's Avatar
    Join Date
    Jun 2003
    Location
    California
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Xsl

    Hello all:

    I have been thinking really hard about how to structure a good CMS and one technology that continues to popup is XSL. I have been weighing the goods and bads of all sorts of different approaches but XSLT is a standard that is easier to understand (for me anyways). Does anyone have any experience w/using XSLT to build a website. What I am planning right now is to have a DB w/the data that will then be reformatted to an XML file that is passed through the XSLT processor and output as XHTML. However, I have a few questions:

    1. Should I create the entire document dynamically or just sections of it. What I mean to say is that should the entire site be built up through the transformation process or should I have a prebuilt document that will output the data I need in its proper places.

    2. Many people use templating engines, are these any better. I have heard that they can become cumbersome and bloat your overall page making things more confusing.

    3. Does the XSLT processor do any kind of tag fixing? Let's say that I have a section of my site that allows users to enter comments about entries that I have made. If they enter invalidly nested tags or something of the like, do I still need some other kind of mechanism for ensuring the proper format (i.e. the Tidy extension)?

    4. If one were to create a site completely dynamically how do you know how it will look to begin with. I typically create a site design in a program like Fireworks and then try to mimic that design with CSS-P and strict web standards (sorry I have been infected by Zeldman and the rest of the standards gurus). Should I continue to do this, and once I have the layout I want, stick the code into my CMS that will output this kind of structure?

    5. I would love to hear how you guys structure your sites.

    I guess I am just confused as to what the advantages/disadvantages there are for the various ways to build a site. You could do everything dynamically, you could build part of the site and let PHP insert the rest, etc.

    Any thoughts/help/opinions would be of great help.

    Thanks,

    - John -

  2. #2
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Norway
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I used it on a project resently and had varius experiences with it.

    First of all XML/XSLT is very powerfull building webapps, and it is the new hot stuff The problem is that you from the start of the project needs to strictly define the xml markup. This is NOT(!) as easy as it sounds if it is your first time. If you take easy on this you will soon end up in a nightmare of xslt templates.

    Second client side support for xslt transformation is only supported in IE and Mozilla (?).

    I would recommend you to build a small site to test the technology. But I would recommend you to go for XML/XSLT even though it can be a bit of a challenge to get starting.

  3. #3
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've built the last few applications on XML / XSLT (as a templating language), and I have to say I'm impressed.

    I'm also confused as anything...

    My approach has been to have my models return "raw" data (PHP arrays / objects), which are assigned in bulk to a dataspace. Once that dataspace is ready to be used by a view, it is converted to xml, by a corresponding XMLTransformer.

    Problem is, for every dataspace I write (simple getters & setters), I have to decide on an XML schema to be used by the XSLT stylesheet. (I don't actually write the schema, but I need to decide on a format which the XSLT scripts will read from...)

    Unfortunately, this makes things rather complicated....

    ... and I'm not sure how to overcome this. Thankfully, its just me working on the project, otherwise it might be a lot worse... (or better?)
    My name is Steve, and I'm a super-villian.

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a prebuilt document that will output the data I need in its proper places.
    Yep, proberly the better option

    Does the XSLT processor do any kind of tag fixing?
    If your XML file has any tags which are either not XHTML compliant, or the tags are not specified within the XSL stylesheet, the processor will ignore them

    I noted this a while back, and you can use it to your own advantage

    I've thought about using XML and XSL-T for building a CMS as well, though I have no time to sit down and design properly

    But I would definitely use it given half the chance, though I would proberly design some kind of abstraction layer for taking the data from the database, and put the data into XML well formed tags yes ?

    That way, the XSL stylesheet would automatically transform a tag to the XHTML for me

    ie
    Code:
    <rows>
    ...
    <row>
    <date>12/03/2004</date>
    </row>
    ...
    </rows>
    And then

    Code:
    ...
    <xsl:template match="date">
    <div><xsl:apply-template /><xsl:select value-of="." />
    </div>
    </xsl:template>
    ...
    You'll have to excuse me if my XSL is off a bit Another template in the stylesheet would for example, transform the XML ROWS and ROW tags into XHTML as well

    Benifit being is that you do not need to 'set' or 'define' your data to a template like Smarty for example ? You just shove the XML string created in the Model into a stylesheet and have PHP process it, spitting out XHTML

    Well, this is the way I see it anyways.

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As an after thought, and something that has been bothering me, is that having for example,

    Code:
    <rows>
    ...
    <date> ... </date>
    ...
    In a Model, would that break layering (MVC) as these are/can be seen as presentation

    Maybe this first needs to be cleared up first ? It would be good to see it as data as well

  6. #6
    SitePoint Zealot ohnnyj's Avatar
    Join Date
    Jun 2003
    Location
    California
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the comments everyone.

    What I was planning on doing was to dynamically create an XML document from data I have stored in a MySQL database that would then be passed to the XSLT processor. However, my main concern is that lets say someone enters the following for a comment:

    This is <a href="#">a<b></a>comment</b>.

    Or something of the like. Will the XSLT processor generate an error, simply strip out all the invalid tags or just let them go through and the will be displayed as they have been entered?

    I want to make sure that any data I get from other users will be transformed into valid XHTML.

    Thanks,

    - John -

  7. #7
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Norway
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    XSLT processors do not use magick, the do what you tell them to Hence, if you haven't said anything about how a tag should be transformed - it simply will be ignored.

    About your example. I would recomend you to specify a xml schema that the input comment must be validated against. That way you can controll what tags that are allowed.

    About MVC and XML/XSLT,
    XML = information
    XSLT = presentation.

    XML has nothing(!) to do with presentation.

  8. #8
    SitePoint Evangelist
    Join Date
    Dec 2003
    Location
    Arizona
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One option might be to have your "Model" classes extend the DomDocument class in PHP 5. This makes content aggregation and transformation a walk in the park.

    JT

  9. #9
    SitePoint Zealot ohnnyj's Avatar
    Join Date
    Jun 2003
    Location
    California
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now I am trying to figure out a good way to structure my xml file. What I have been thinking so far is thus:

    HTML Code:
     <?xml version="1.0" encoding="iso-8859-1"?>
      <blog>
      	<entry id="1">
      		<link>[url="http://www.somewhere.com#1"]http://www.somewhere.com#1</link>[/url]
      		<title>Title Goes Here</title>
      		<date>07.15.04</date>
      		<subtitle>Subtitle Text<subtitle>
      		<author>
      			<firstname>John</firstname>
      			<lastname>Smith</lastname>
      		</author>
      		<username>uname</username>
      		<excerpt>Summary type text here.</excerpt>
      		<content>The entry text goes here.</content>
      	</entry>
      </blog>
    Then for a stylesheet should I have something like such:

    HTML Code:
    <?xml version="1.0" encoding="UTF-8" ?>
     <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     	<xsl:output method="html" />
     	<xsl:template match="entry">
     		<div id="
     			<h3><xsl:apply-templates select="content" /></h3>
     		</div>
     	</xsl:template>
     	<xsl:template match="content">
     		<p><xsl:apply-templates /></p>
     
     	</xsl:template>
     </xsl:stylesheet>
    Obviously this is a simplified version that will be expanded.

    What I was planning is to populate an XML file like the one here with fields I have stored in a db, then send that XML file through the stylesheet as defined to get my XHTML.

    Please let me know if I am way off base or what.

    Thanks,

    - John -

  10. #10
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    For a blog, why not use a predefined XML standard like RSS, rather than reinventing the wheel? You'll probably want to generate RSS anyway (it's a blog after all ), so might as well start with that and transform it to XHTML.

  11. #11
    SitePoint Zealot ohnnyj's Avatar
    Join Date
    Jun 2003
    Location
    California
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that's exactly what I was thinking in the first place but someone had mentioned to create my own scheme.

  12. #12
    SitePoint Zealot ohnnyj's Avatar
    Join Date
    Jun 2003
    Location
    California
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about RDF?

    I'm sorry I must be stupid, I guess RDF is just another RSS standard.

    I guess the question then becomes which version should I output to, or does it matter?

    - John -

  13. #13
    SitePoint Member
    Join Date
    Oct 2001
    Location
    United Kingdom
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ohnnyj
    What about RDF?

    I'm sorry I must be stupid, I guess RDF is just another RSS standard.

    I guess the question then becomes which version should I output to, or does it matter?

    - John -
    1.0 and 2.0, atom aswell.

    Thats why its good to define your own schema. Remember RSS is only a summary (where will you put your comments?).

  14. #14
    SitePoint Zealot ohnnyj's Avatar
    Join Date
    Jun 2003
    Location
    California
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So should I have a modified RSS xml file? Some sort of hybrid that combines the original RSS and my own tags?

    Thanks,

    - John -

  15. #15
    SitePoint Member
    Join Date
    Oct 2001
    Location
    United Kingdom
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No create your own!

    Code:
    <news>
      <entry id="" permalink="">
        <title></title>
        <author></author>
        <published></published>
        <modified></modified>
        <summary></summary>
        <![CDATA[
            <!-- Your content here. -->
        ]]>
        <comments count="">
          <comment id="">
            <author>
              <name></name>
              <email></email>
              <site></site>
            </author>
            <![CDATA[
              <!-- Comment body here. -->
            ]]>
          </comment>
        </comments>
      </entry>
    </news>
    It's much easier to look at. Thats what XML is all about

  16. #16
    SitePoint Zealot sleepeasy's Avatar
    Join Date
    Sep 2003
    Location
    Bristol, UK
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rw22
    No create your own!

    Code:
        <comments count="">
    Just nitpicking:

    You won't need the count attribute as you can just use XSL's count() function when you transform the XML.

    Code:
      <xsl:template match="news">
        ...
          <xsl:for-each select="entry">
            ...
            <p><xsl:value-of select="count(comments/comment)"/> Comments</p>
        </xsl:for-each>
      </xsl:template>
    Always open to question or ridicule

  17. #17
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Norway
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ups... Delete me.

  18. #18
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Norway
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would recommend you to have a tag for links, somthing like this:
    Code:
    <link>
      <linkName>My link</linkName>
      <title>This is a test link.</title>
      <target>http://www...</target>
    </link>
    This way you can use a template like this to transform all links:

    Code:
    <xsl:template match="link">
      <a>
        <xsl:attribute name="href"><xsl:value-of select="target"/></xsl:attribute>
        <xsl:attribute name="title"><xsl:value-of select="title"/></xsl:attribute>
        <xsl:value-of select="linkName"/>
      </a>
    </xsl:template>

  19. #19
    SitePoint Evangelist
    Join Date
    Dec 2003
    Location
    Arizona
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here are some links I post often when someone brings up XML/XSLT-based Web applications:

    http://xao-php.sourceforge.net/ctrl....ing_pty-vs-std

    http://www.sitepoint.com/forums/show...plified+Syntax

    JT

  20. #20
    SitePoint Zealot sleepeasy's Avatar
    Join Date
    Sep 2003
    Location
    Bristol, UK
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Psi_^
    I would recommend you to have a tag for links, somthing like this:
    Code:
    <link>
      <linkName>My link</linkName>
      <title>This is a test link.</title>
      <target>http://www...</target>
    </link>
    This way you can use a template like this to transform all links:

    Code:
    <xsl:template match="link">
      <a>
        <xsl:attribute name="href"><xsl:value-of select="target"/></xsl:attribute>
        <xsl:attribute name="title"><xsl:value-of select="title"/></xsl:attribute>
        <xsl:value-of select="linkName"/>
      </a>
    </xsl:template>
    I'm going to do it again, but only because it may save you some typing. You may know that you can use {xpath} for attribute values instead of the xsl:attribute element - if you do then please excuse me - I'm just trying to help

    You could have written your template above:
    Code:
    <xsl:template match="link">
      <a href="{target}" title="{title}"><xsl:value-of select="linkName"/></a>
    </xsl:template>
    Always open to question or ridicule

  21. #21
    SitePoint Zealot cholmon's Avatar
    Join Date
    Mar 2004
    Location
    SC
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using XSLT as a template engine, a la smarty, is something i've been screwing around with alot lately. While my experience with smarty is pretty shallow, I think XSLT can handle most of the basic presentation type functionality. variable substitution, iterating over a set of data, conditional statements, etc...and having it as a well defined, cross-platform technology is a nice little fact as well.

    As seratonin has suggested, using the DomDocument to create your own models (assuming an MVC arch.) makes it very easy to populate the model with valid XML markup, similar to the way smarty "assigns" PHP variables to a smarty object.

    As far as the overall structure of the site goes *takes a deep breath*, I've been mulling over different ways to fit all this stuff together, mainly with regard to the presentation. Until a few months ago (when I decided to actually start learning XSL), I stuck to the "header-content-footer" method of building pages that everyone in the world uses:

    about.php
    PHP Code:
    <?php require('header.inc.php'); ?>

    ...about cholmon...

    <?php require('footer.inc.php'); ?>
    The thing that drew me to XSL, however, was the fact that there are few things I hate more than writing a bunch of PHP/HTML/PHP/HTML/PHP to display a giant result set from MySQL. Even when I use CSS to simplify the HTML that needs to be generated, it still gets on my nerves. Other things, like conditionally include()ing or require()ing ANYTHING made me giddy when I realized how much easier XML/XSLT can make the whole process. I don't know why I waited so long to try to learn this stuff.

    Anyway, I started loading all my data into DomDocuments, then did the transformation, and all of the sudden XSLT was my new friend...until I realized that I was writing a single stylesheet for each page. How was I supposed to include a common header and footer for each page? After finding the <xsl:include> tag, I changed the structure of the page from having about.php just transform the DomDocument with about.xsl to display the output, to having about.xsl first include main.xsl for all the header and footer stuff:

    main.xsl
    Code:
    <?xml version="1.0" ?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="/">
        <html>
        <body>
          <xsl:apply-templates select="content"/>
        </body>
        </html>
      </xsl:template>
    </xsl:stylesheet>
    about.xsl
    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:include href="main.xsl" />
    
      <xsl:template match="content">
        all about......something
      </xsl:template>
    </xsl:stylesheet>
    This way I could just include main.xsl at the top of each stylesheet, and they'd all have the same outer structure. Obviously both main.xsl and about.xsl have much more stuff in them, and main.xsl assumes a relatively basic overall page structure (ie, a title/logo bar, a navigation menu, a content section, etc). Those page elements, hoewver, can be shaped however I want, and placed wherever I want...as long as each page in the site can fit into that basic structure.

    as far as the XML that gets parsed through these templates, I'm still refining the way it's structured. I don't have any kind of formal schema, but it is essentially built like this (in memory):

    $xml (DomDocument object)
    Code:
    <?xml version="1.0" ?>
    <page>
      <title/>
      <stylesheets/>
      <scripts />
      <navigation>
        <nav id="home" link="index.php">Home</nav>
        <nav id="about" link="about.php">About</nav>
        <nav id="contact" link="contact.php">Contact</nav>
      </navigation>
      <content/>
    </page>
    Actually, since every page will share some of the same elements, like the nav options, I keep this XML in an actual file, called core.xml. when a page like about.php is hit, it starts by doing whatever set up stuff it needs to...like hitting the DB for content. Then it loads core.xml into a DomDocument, and adds child elements to elements like <title> and <content>. FOr example, once about.php is ready to send the XML through XSLT to display the page, the XML is modified to look like:

    $xml (DomDocument)
    Code:
    <?xml version="1.0" ?>
    <page>
      <title>About Cholmon</title>
      <stylesheets>
        <css>stylesheet.css</css>
      </stylesheets>
      <scripts />
      <navigation active="about">
        <nav id="home" link="index.php">Home</nav>
        <nav id="about" link="about.php">About</nav>
        <nav id="contact" link="contact.php">Contact</nav>
      </navigation>
      <content>
        <paragraph>
           blah blah blah blah blah blah blah blah blah blah 
        </paragraph>
        <paragraph>
           yackity schmackity yackity schmackity yackity schmackity 
        </paragraph>
      </content>
    </page>
    Notice how <navigation> became <navigation active="about">...that lets the 'About' link like show up as the active link. How that is actually done (CSS?) depends on how main.xsl is told to parse the <nav> elements.

    Also, the <content> element of core.xml can end up containing anything I want, in this case some <paragraph> elements...as long as about.xsl has a template to handle <paragraph> elements.

    crap that's way more than I intended to write. this whole idea o' mine is obviously still in infancy, but there are a few things I have in mind to make things a little bit smoother/faster. Caching the DomDocument, for instance, would help. I've also given some thought to using SimpleXML instead of DomDocument in order to simplify adding and changing elements that are loaded from core.xml...any ideas on that front?

    Questions and comments are much appreciated...I haven't really gotten anyone else's opinions on all this stuff yet.

  22. #22
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Consider me to be thick, but what is it with RDF huh ? Never really understood this, maybe someone can tell me the difference (and benfits if any) of RSS and RDF

  23. #23
    SitePoint Member
    Join Date
    Oct 2001
    Location
    United Kingdom
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sleepeasy
    Just nitpicking:

    You won't need the count attribute as you can just use XSL's count() function when you transform the XML.
    Of course! But if you had a listing of entries you wouldn't really require the comments for each one, so i would replace the comments block with:

    Code:
    <comments count="12" />
    I just got a little confused with what i was doing.

  24. #24
    SitePoint Member
    Join Date
    Oct 2001
    Location
    United Kingdom
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cholmon
    $xml (DomDocument)
    Code:
    <?xml version="1.0" ?>
    <page>
      <navigation active="about">
        <nav id="home" link="index.php">Home</nav>
        <nav id="about" link="about.php">About</nav>
        <nav id="contact" link="contact.php">Contact</nav>
      </navigation>
    </page>
    Couldn't you have a <page id="contact"> and then match that to the navigation; or maybe a <nav id="contact" link="contact.php" active="true">.

  25. #25
    SitePoint Zealot cholmon's Avatar
    Join Date
    Mar 2004
    Location
    SC
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rw22
    Couldn't you have a <page id="contact"> and then match that to the navigation; or maybe a <nav id="contact" link="contact.php" active="true">.
    oh sure...as long as the XSL template knows how to find which nav option needs to be active.
    • <page id="contact"> would work, but since the 'id' attribute is only used to match <nav> options, it would make more sense to put it closer to where the <nav> is defined.
    • <nav id="about" link="about.php" active="true">About</nav> is easier to understand...and actually, i might use this instead, since the XPath in the XSL would be a little bit cleaner. so in the template for <nav>, instead of <xsl:if test="//navigation/@id = @id"> I could write <xsl:if test="@active = 'true'">


    thanks for the input


Bookmarks

Posting Permissions

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