Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> XSLT Beginner
virtuous8
post Mar 15 2006, 06:31 AM
Post #1


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 10
Joined: 13-March 06
Member No.: 11,941



start with an xml file:

CODE
<?xml encoding="utf-8" version="1.0"?>

<root>
  <contact type="business">
     <name>John</name>
  </contact>
  <contact type="business">
     <name>Jane</name>
  </contact>
  <contact type="business">
     <name>Jack</name>
  </contact>
  <contact type="family">
     <name>Jim</name>
  </contact>
</root>



now write your contacts.xslt file:

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="xml" encoding="utf-8" />    
<xsl:template match="/">
  <html>
  <xsl:for-each select="//contact/[type=business]">
    <xsl:call-template name="contact" />
  </xsl:for-each>    
  </html>
</xsl:template>
    
<xsl:template name="family">
   <xsl:value-of select="./name" />
   <br/>
</xsl:template>

</xsl:stylesheet>



now you need xslt processor to combine xml + xslt, in php there is get Sablotron xslt processor.
example using Sablotron:

CODE
<?php
    $xmlFile = 'contacts.xml';
    $xslFile = 'contacts.xslt';
    // Create a new processor handle
    $xslt_processor= xslt_create() or die("Can't create XSLT processor handle!");
    //set up parameters
    //$parameters = array('searchstring'=>$searchstring);
    $parameters = NULL;
    //perform the XSLT transformation
    $result = xslt_process($xslt_processor, $xmlFile, $xslFile, NULL, NULL, $parameters);
    // Free up the resources
    xslt_free($xslt_processor);
    echo($result);
?>


the output is:
John
Jane
Jack


notes and explanation:
xslt uses powerful xpath expression to navigate through the xml parsetree.
Go to the top of the page
 
+Quote Post
FirefoxRocks
post Jun 13 2008, 03:28 AM
Post #2


Super Member
Group Icon

Group: [HOSTED]
Posts: 660
Joined: 12-July 06
From: Ontario, Canada
Member No.: 14,464



First of all, you do not need to use server side XSLT parsing to read an XML file. You can simply specify it as a stylesheet using an XML processing instruction in the XML file like this:
CODE
<?xml-stylesheet type="text/xsl" href="yourFile.xsl" ?>
.

Second of all, for a beginner tutorial, I wouldn't make it as complicated with attribute names and multiple templates. I mean <xsl:call-template />? That is definitely not for beginners.

Internet Explorer has the MSXML parser, Gecko-based browsers (Firefox/Mozilla/Flock) has a built-in XSLT parser and Safari/Konqueror can also read XSLT correctly too. You don't need PHP to parse XSLT, browsers can do it.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Mp3 Questions(14)
  2. A Beginner Wanting To Learn How To Make A Web Game(4)
  3. Help A Beginner Start On With Game Programming(8)
  4. Javascript Tutorial For Beginner(0)
  5. Sig Tutorial: Smudging And Filters(10)
  6. Guide For Beginner Convert Video To Psp(2)
  7. Javascript Xml Parser Vs. Xslt Processor(0)
  8. Beginning Web Designer Templates & Logos(4)
  9. Php 101: Php For The Absolute Beginner(2)
  10. Suggest Books/tutorials For Solaris 10 Beginner(2)
  11. Resources For Beginner Delphi Programmer ?(11)
  12. How To Setup Your Own Server(8)
  13. What Are Your Favorite JSP Book(s)(3)
  14. How Can You Spice Up Your Basic HTML Site ? Beginner Needs Help(9)
  15. Xslt Sablotron(4)
  1. Help A Beginner To Learn To Use Linux Terminals(9)
  2. Any Beginner Tutorials On Fedora Core 4 ?(4)
  3. Css Vs Xslt Vs Smarty Vs Other : What To Use ?(3)
  4. Help With Xml+xslt Needed...(9)
  5. Whats The Best Tool For A Beginner?(17)
  6. Xslt Sablotron(1)


 



- Lo-Fi Version Time is now: 7th September 2008 - 10:50 PM