www.webdeveloper.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2008
    Location
    Maryland
    Posts
    38

    PHP XSL Translate

    Sorry for the long post from a newbee! I’m in the process of moving the sites I have running on a windows ASP web server to a Linux PHP web server. A lot of my pages have variable data that is stored in XML files. I have XSL files that will convert the data into web page format, but I’m having trouble getting the Linux server to do the translate. Here is the ASP code that I use to do the translate in-line:

    The page.asp file would use a server side include to place the xml data where desired:
    Code:
     <TR>
       <TD><!--#include file="ShowXml.inc"--> </TD>
       <TD><IMG HEIGHT="238" WIDTH="277" SRC="image.jpg"></TD>
     </TR>
    The ShowXml.inc file contains the ASP code to display the XML data:

    Code:
    <% 
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''  Function to display the XML with XSL translation   '' 
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Function ShowCatalog
    
    Dim xml
    Dim xsl
    Dim xslname
    
    xslname = "xdb/MyFormat.xsl"
    
    'Load XML
    set xml = Server.CreateObject("Microsoft.XMLDOM")
    xml.async = false
    xml.load(Server.MapPath("xdb/MyData.xml"))
    
    'Load XSL
    set xsl = Server.CreateObject("Microsoft.XMLDOM")
    xsl.async = false
    xsl.load(Server.MapPath(xslname))
    
    'Transform file
    Response.Write(xml.transformNode(xsl))
    
    set xml = Nothing
    set xsl = Nothing
    
    End Function
    
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''                    Main Process                     '' 
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    'Do not break on an error.
    On Error Resume Next
    
    ShowCatalog
    
    End If
     %>
    OK, since I’m asking for some help with the PHP coding, let me give a little demo of the XML / XSL coding in return.

    The XML file might look like this:


    Code:
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="MyFormat.xsl"?>
    <events>
    	<event>
    		<id>1</id>
    		<column>1</column>
    		<index>080409</index>
    		<date>April 9, 2008</date>
    		<link>http://www.yahoo.com</link>
    		<line1>Yahoo</line1>
    		<line2>A search Engine</line2>
    	</event>
    	<event>
    		<id>2</id>
    		<column>2</column>
    		<index>080405</index>
    		<date>April 5, 2008</date>
    		<link>http://www.google.com</link>
    		<line1>Google</line1>
    		<line2>Another search engine</line2>
    	</event>
    </events>
    The XML file contains a stylesheet reference to the XSL file that I will be using. That way when I bring it up on my local browser it will do the translate so I can see the results. The top-level events item contains multiple event items. Each event has an “id” so it can be referenced by an administration function (that one might be my next question, but first things first). The “date” which will be displayed relates to the “index” which is used for sorting the events in each “column” of the final table. The “link” is used as the navigation point that surrounds the multiple “lines” of display.

    Here is the XSL file that converts the events into a two-column table of data:
    MyFormat.xsl

    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <table class="Events" border="0">
        <tr>
          <td class="EventCol" valign="top">
            <xsl:for-each select="/Events/event">
              <xsl:sort select="index" order="ascending"/>
              <xsl:if test="column = '1'">
                <div class="EventDate"><xsl:value-of select="date"/></div>
                <xsl:if test="string-length(link)">
                  <a HREF="{link}" TARGET="">
                  <xsl:if test="string-length(line1)">
                    <xsl:value-of select="line1"/>
                  </xsl:if>
                  <xsl:if test="string-length(line2)">
                    <br/><xsl:value-of select="line2"/>
                  </xsl:if>
                  <xsl:if test="string-length(line3)">
                    <br/><xsl:value-of select="line3"/>
                  </xsl:if>
                  </a><br/><br/>
                </xsl:if>
              </xsl:if>
            </xsl:for-each>
          </td>
          <td class="EventGut"/>
          <td class="EventCol" valign="top">
            <xsl:for-each select="/Events/event">
              <xsl:sort select="index" order="ascending"/>
              <xsl:if test="column = '2'">
                <div class="EventDate"><xsl:value-of select="date"/></div>
                <xsl:if test="string-length(link)">
                  <a HREF="{link}" TARGET="">
                  <xsl:if test="string-length(line1)">
                    <xsl:value-of select="line1"/>
                  </xsl:if>
                  <xsl:if test="string-length(line2)">
                    <br/><xsl:value-of select="line2"/>
                  </xsl:if>
                  <xsl:if test="string-length(line3)">
                    <br/><xsl:value-of select="line3"/>
                  </xsl:if>
                  </a><br/><br/>
                </xsl:if>
              </xsl:if>
            </xsl:for-each>
          </td>
        </tr>
      </table>
    </xsl:template>
    </xsl:stylesheet>
    Note how the html coding is interspersed with xsl commands. The data is selected by event, it is then sorted by the “index” YYMMDD, then tested for column 1 and column 2 of the table. The xsl:value-of is used to insert the xml data into the html stream. Note the use of curly braces instead of the value-of syntax to insert the link inside an html command.


    I would be interested in exchanging e-mail addresses with anyone who would like to know more about css, xml and xsl in return for more help with php.

    Thanks in advance for the assistance,

    PapaGeek
    Last edited by PapaGeek; 01-28-2009 at 09:36 AM.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,102
    You might want to check if your server has the XSL and/or XSLT extension enabled and see if it could do the job for you. (I've not worked with either extension, so cannot really give you any specific advice about them. This tutorial looks like it might be a good start.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles