XSLT Beginner

free web hosting
Free Web Hosting > Computers & Tech > How-To's and Tutorials > Programming > HTML, XML and other Markup Languages

XSLT Beginner

virtuous8
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.

 

 

 


Reply

FirefoxRocks
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.

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. xslt vs javascript - 94.85 hr back. (1)
Similar Topics

Keywords : xslt beginner


    Looking for xslt, beginner

Searching Video's for xslt, beginner
advertisement




XSLT Beginner



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE