Software is like sex... It's better when it's free.

Blog - Tag 'xml'

As everyone has seen, this is not easy to find time to write news but ..

Here it is, the year 2011 began, the perfect opportunity to make a first report after more than three years spent in intensive web development alongside SillySmart and now Wandi.

3 ans de développement Web

The first year of Wandi was very rich with the completion of more than 30 websites.

A big thank you to all those who trust us and allow to advance the adventure with Wandi and SillySmart.

Not forgetting all those who are with us every day: the Wandiz, Charlie, Jeremy, Mickey, Chatton, Nsn guys and all others..

Happy New Year !
Tags:  ajaxjsonmysqlphpsillysmartwandixmlxpathxsl.
Posted the Monday 03 january 2011 15:56:16 - 0 comment

If you are a beginner in XSL, beyond the basic features (select, for-each, choose, if variable), you have been surprised not to find a simple for loop, while the for-each was present, but the for-each does not always do everything. So if you want to make a small loop for several solutions available to you. To illustrate, we will take a simple example: a mark/5 on article (float) on the fact that you have this mark this in your XML feed, and you want to display the number of stars corresponding to this mark.

1st solution you are using PHP5 and using the registerPHPFunctions() of class XSLTProcessor(), you can simply use one of your PHP functions to carry out the treatment.

<?php
$xml = new DOMDocument()
$xml->load('flux.xml');
$xsl = new DOMDocument()
$xsl->load('flux.xsl');
$proc = new XSLTProcessor();
$proc->registerPHPFunctions();
$proc->importStyleSheet($xsl); 
echo $proc->transformToXML($xml);

function displayStars($note)
{
  $html = "";
  for($i=0 ; $i<floor($note) ; $i++)
    $html .= '<img src="Public/Styles/Img/star.gif" title="'.floor($note).'/5" />';
  echo $html;
}
?>

Thus, in XSL, simply call your PHP function with php xmlns without forgetting the attribute disable-output-escaping to interpret HTML returned your function.

<!--
- Test with XSLTProcessor
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl" version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
  <xsl:template match="root">
    <html>
      <head></head>
      <body>
        <xsl:value-of select="php:function('displayStars',//article/note)" disable-output-escaping="yes" />
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

The 2nd solution is based solely on XSL and involves the use of recursive templates to simulate a for loop.

<!--
- Recursive template
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl"" version="1.0">
  <xsl:template name="displayStars">
    <xsl:param name="i" />
    <xsl:param name="length" />

    <!-- For Treatment -->
    <img src="Public/Styles/Img/star.gif" alt="{concat($length,'/5')}" title="{concat($length,'/5')}" />
    <!-- /For Treatment -->

    <!-- While length is not reached, we recalled the template to continue the loop -->
    <xsl:if test="$i < $length">
      <xsl:call-template name="displayStars">
        <xsl:with-param name="i" select="$i + 1" />
        <xsl:with-param name="length" select="$length" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

Whether we call it like this:

<!--
- Call recursive template
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl" version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
  <xsl:template match="root">
    <html>
      <head></head>
      <body>
        <xsl:call-template name="displayStars">
          <xsl:with-param name="i" select="'1'" />
          <xsl:with-param name="length" select="round(//article/note)" />
        </xsl:call-template>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Note that even if the for loop in XSL is exploitable, it quickly becomes tedious redevelopment of templates specific to each processing loop.

A 3rd solution, for large projects, if we keep the same example of the mark, is to arrange to slightly modify the structure of its formed XML as PHP to achieve its treatment with only the for-each.

Here, instead of training:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <article>
    <note><![CDATA[3.4]]></note>
  </article>
</root>

It might have been formed:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <article>
    <note>
      <item><![CDATA[]]></item>
      <item><![CDATA[]]></item>
      <item><![CDATA[]]></item>
    </note>
  </article>
</root>

where the number of item corresponding at floor() php in order to "foreach" quietly on our marks and simulate a for loop:

<!--
- Fake for-each
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl" version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
  <xsl:template match="root">
    <html>
      <head></head>
      <body>
        <xsl:variable name="note" select="count(//article/note/item)" />

        <xsl:for-each select="//article/note/item">
          <img src="Public/Styles/Img/star.gif" alt="{concat($note,'/5')}" title="{concat($note,'/5')}" />
        </xsl:for-each>

      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

You can see several solutions that allow you to make your loop, you are free to choose what suits you best, with your technical envirronnement and your choice of architecture.

Tags:  phpxmlxsl.
Posted the Wednesday 06 may 2009 19:32:22 - 1 comment

For fans of the XML/XSL/XPath combo, a small trick when you push it the limits of generic to form an XPath expression dynamically.

Suppose a xsl variable contains an "end" of the XPath expression (one node), this would give us this:

<!--
  - Select a xml node with a dynamic xPath expression
  -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl" xmlns="http://www.w3.org/1999/xhtml">
  <xsl:template match="root">
    <xsl:variable name="dynamicNode" select="'user_name'" />

    <!-- this will give this xPath = "//root/bdd/users[1]/user_name" -->
    <xsl:value-of select="bdd/users[1]/*[name()=$dynamicNode]" />

  </xsl:template>
</xsl:stylesheet>
Tags:  xmlxpathxsl.
Posted the Tuesday 28 april 2009 20:39:02 - 0 comment

SillySmart is a lightweight and flexible MVC Framework written in PHP5 based on XML/XSL's parsing.

SillySmart is a free, open-source Web application development framework since 2007.

It was written to address several issues:

  • No PHP framework actually allows a natural way to use a View layer built through XML/XSL.
  • Allowing various collaborators on a project with different and complementary skills to work independently at several levels of the code (PHP at Controller's level, MySQL at Model's level, XSL at View's level, client-side JavaScript, etc.) ; without interfering or depending on your collaborator’s works.
  • Implement a true separation of MVC layers across various technologies and specific language to each layer
  • Allow a readable implementation of layer View which takes advantage of a “phtml templates” like semantic in XML, to obtain with modularity, a powerful progressive and corrective maintenance dimension.
  • Provide a framework, that thanks to his XML skeleton can easily generate N distinct types of outputs (RSS, Atom, Web services, Ajax, etc..) without any problems of casting or parsing.
  • Provide a lightweight framework that doesn't carry hundreds of components but still stays expandable to meet individual needs through additional plugins.

Main features:

  • MVC
  • XML / XSL / xPATH
  • DAO / Persistence / CRUD
  • PDO / QueryBuilder / ExprBuilder
  • i18n
  • HMI
  • Admin Generator
  • Theming 
  • URL Rewriting / Disptaching / URL Mapping
  • Multi domains
  • Multi databases
  • Plugins
  • many mores...

Check http://www.sillysmart.org for full presentation, download, api, getting started, documentation and forum.

Tags:  frameworkmvcphpxmlxsl.
Posted the Wednesday 22 august 2012 17:36:29 - 0 comment
If you want to display a list of entities using XSL and css styles in table rows, you can play on modulos using the function position() in XSL.

Suppose the following XML feed listing a set of users:

<?xml version="1.0" encoding="UTF-8"?>
<users>
  <user>
    <id>1</id>
    <login>lo</login>
    <email>laurent@sillysmart.org</email>
  </user>
  <user>
    <id>2</id>
    <login>flo</login>
    <email>florian@sillysmart.org</email>
  </user>
  <user>
    <id>3</id>
    <login>charly</login>
    <email>charly@sillysmart.org</email>
  </user>
</users>

The following css :

table.bo_table_visible {	
  border:1px solid #000;
  border-spacing: 0px;
  margin-top:10px;
  margin-bottom:10px;
}
table.bo_table_visible tr.summary th {
  background:#2E2D2D;
  color:#E45A49;
}
table.bo_table_visible tr td {
  padding:0 10px;
}
table.bo_table_visible tr.even td, table.bo_table_visible tr th {
  background-color:#FFF;
  color:#000;	
}
table.bo_table_visible tr.even:hover td {
  background-color:#FF8ABC;
  color:#FFF;
}
table.bo_table_visible tr.odd td {
  background-color:#DFDFDF;
  color:#000;
}
table.bo_table_visible tr.odd:hover td {
  background-color:#64B8FE;
  color:#FFF;
}

Simply apply the XSL script below to apply styles to your list of users:

<!--
- Apply css with XSL
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl" version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
  <xsl:template match="root">
    <html>
      <head></head>
      <body>
        <table class="bo_table_visible">
          <tr>
            <th>Id</th>
            <th>Login</th>
            <th>Email</th>
          </tr>
          <xsl:for-each select="//users/user">
            <xsl:variable name="cssClass">
              <xsl:choose>
                <xsl:when test="(position() mod 2) = 0">
                  even
                </xsl:when>
                <xsl:otherwise>
                  odd
                </xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
            <tr class="{$cssClass}">
              <td><xsl:value-of select="id" /></td>
              <td><xsl:value-of select="login" /></td>
              <td><xsl:value-of select="email" /></td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Will produce :

Id Login Email
1 lo laurent@sillysmart.org
2 flo florian@sillysmart.org
3 charly charly@sillysmart.org
Tags:  cssxmlxsl.
Posted the Tuesday 08 september 2009 17:20:44 - 0 comment