Archive for the ‘XSLT’ Category

XSLT library for Umbraco: update

Some 2 years ago I published an xslt library extension for Umbraco. I added several methods to the class that proved to be useful for me, maybe they are useful for you too.

The code is provided as is, no garanty, blabla … though should not harm anyone.

You can download the dll and adjust your xslt configuration settings in Umbraco or download the full VS2008 project [1.5 MB] and have a look at the code.
Just for the reference, below is a list of methods available in the class with a short description

XPathNodeIterator QueryDatabase(string conn, string query )
Query the umbraco database or any other database that you need.
string createMemberGroup(string memberGroupName)
Create a member group
string isMemberOfGroup(string memberGroupName, string memberId)
Check if the member supplied belongs to the group supplied
XPathNodeIterator GetAccessingGroups(int documentId)
Not working, will be removed …
XPathNodeIterator getAllMembersFromGroup(string groupId)
REturn all the members that are in a group
string getMemberGroups()
Returns the list of groups the current memner belongs to
XPathNodeIterator getAccesXml()
Returns the full xml document that contains the list of protected pags and groups.
string getMemberFromFirstLetter(char letter)
Well, get member from the first letter
XPathNodeIterator getMemberFromFirstLetterXml(char letter)
Well, same, but then in xml.
string addMemberToGroup(string memberGroupName, int memberId)
Add a given member to a give group
string addPageToGroup(int currentPage, int groupId, int loginPage, int errorPage )
Protect a page with a membergroup
bool updateProperty(int DocumentId, string propName, string propValue)
Update any property (not the umbraco properties) for any given document.

Download

XSLT and PHP on a Mac, Part 2

Now that I installed MySQL, got the apache server running and Wordpress installed, I am facing another problem.

The version of PHP included in Mac OS X seems to be an old one (that’s fine) without XSL transformations support (not ok) as it is not compiled into the module.

The one programming language I continuously use during the day is XSLT combined with a lot of XML documents and webservices. I need XSL, really, and I cannot live without XSL support, no way.

I could download the source code, find all the libraries, find the commands to compile and fix everything manually. But it is just easier to download the PHP package from Marc Liyanaga.

First you should disable the PHP4 module in the Apache config file, because if you install on top of that it will give you some strange errors. It will not remove the Apple module, but Apache will try to load both modules and that is something to avoid.

Download the package [PHP 5.2.4 for Apache 1.3], install and try the phpinfo file you created before, and you will see that you have a running version of PHP5.
I checked the configuration and it seems to have XSL support.

None of the code I wrote before ran without problems, everything resulted in a fatal error. After some time I figured out that the XSL module has changed extensively
As a result of that none of the code I wrote before will work, ever again.

At the moment I have PHP4 without XLST support installed, and a PHP5 version with XSLT support.

I tried Fink with the Finkcommander but they only have PHP modules for Apache 2, so no luck with that either.

This might not be a big problem for you, but I want to make sure the plugin I am writing, works on as many platforms as possible. and that includes PHP4.
I could not find any statistics on how many hosts have switched to PHP4, I think not they are many.

The PHP website mentions that PHP4 will no longer be supported from January 2008, so I guess I am going to develop for PHP5 and hope for the better.

Performing loops in XSLT

If you read this blog from time to time, you might have noticed that I am often talking about the XML/XSLT combo. Some people really don’t like declarative programming languages, because it is well, declarative. It works for me though.

(http://en.wikipedia.org/wiki/Declarative_programming). (Dimitre Novatchev actually proved [PDF 400kb] that XSLT can be used as a functional programming language)

One of the problems with XSLT is looping. To create a loop, you have to be able to update the same variable over and over again until you reach the desired value for that variable.
But XSLt doesn’t do that. if you declare a variable it will hold the same value untill the end. this bit really puzzles alot of people, but wait there is a solution.

You can get around almost any problem because most of the XSLT processors available, allow you to extend your XSLT stylesheet with JSCript, C# or whatever compiler is available on your system. But it is a bit messy.

If you start using this kind of combination of JavaScript and XSLT, your code gets unreadable very soon. So that is the reason why Oliver Becker created this nice loop extension. He uses nothing but XSLT that can be used as any other XSLT Extension. Have a look at the code below how it’s done:

<loop:for name="i" from="10" to="1" step="-1">
 <loop:for name="j" from="1" to="10">
  <xsl:value-of select="$i * $j" />
   <xsl:text>&#x9;</xsl:text>
  </loop:for>
 <xsl:text>&#xA;</xsl:text>
</loop:for>

He implemented for loop AND while loops.

<loop:while test="expression">
 <!-- optional some <xsl:variable> elements -->
  <loop:do>
   <!-- template body -->
    </loop:do>
    <loop:last>
   <!-- template body -->
  </loop:last>
 <!-- at least one <loop:update> element -->
</loop:while>

And a “function” to just update a variable:

<loop:update name="qname" select="expression" />

Have a look at the other XSLT stylesheets he is offering for download on his homepage:

  • Merging XML documents
  • Tracing execution of a stylehseet
  • Quine: The Self-Reproducing Stylesheet

Extending Umbraco with a library

I have been using the .net based Umbraco CMS for more then a year now and never have I had the feeling it limited me in anyway. But now I need a quick and dirty approach to connect to a database other then the Umbraco database.

Umbraco can grab XML feeds using the build in XSLT library extension, but I am just playing around at the moment and need direct access to manipulating query results.

So I created my own XSLT library. It has one 1 method at the moment: QueryDatabase(‘connection string’,'query’)

Just a note: it probably is not the greatest idea to use this in a production environment. What I plan to do afterwards, is to hard-code the query into another method not allowing any unknown request to the database.

But here is the code anyway (code pasted below), the dll and the VS 2003 project zip file.

If you want to use it without looking at the code just do the following actions:

  1. Download the astuanax xslt libarary dll
  2. Unzip the file and put it in the “/bin” folder of your Umbraco website
  3. Edit the file “xsltExtensions.xml” in the folder “/config” and
  4. Add the following line:
  5. Once you got this running, go to the XSLT editor in Umbraco and paste the following code into your xslt file:

    This will show you the XML code you can expect from this query.

Leave a comment if you like it. Or if you have a problem using it, let me know and I will try to fix it.

C# Code:

Quality control with style sheets

Once you are off with a CMS and people start to edit pages, all sorts of code grows inside the database. Copy-paste leftovers from MS Word and code handwritten by tech enthusiast clutter your clean and simple HTML template and after 2 weeks your design is no longer what it used to be.

Sounds familiar?

To avoid all these problems we can use a well-known CSS combo: dependency and specificity, to mark all these little coding errors with big borders in orange or red, depending on the severity. Editors really don’t like these boxes and will do whatever it takes to get them out.
To see how it works you need a human editor with HTML3 and <FONT> tag knowledge, a CMS that can detect if an editor is logged on (any CMS does that), and the stylesheet we are going to create.

Editors in charge

You or a designer crafted the headers very carefully, and they really look great in the new design. Pixel-perfect!
Unfortunately the editor in charge thinks that headers should be bigger and bolder. He looks at the design and goes into the CMS to make same basic changes, adding FONT, B and any other element he knows about. This is additional markup that can mess up your entire website and really shound’t be in there.

There is actually not much you can do about it if you stick to using a rich text editor (RTE) and don’t want to break down the document into distinct fields (and validate these fields). But… you can make people aware of these things. And I will show you can do this using a few lines of code and a stylesheet.

Let’s have a look at an example stylesheet:

/* stylesheet headers */
h1 {	}
h2{
	font-size: 1.05em /* not too big */
	font-weight: normal; /* instead of bold */ }
h3 {	}

What happens if the editor incorrectly adds a <FONT> tag, and a B element?


What happens here? If you are unlucky, the rest of the page is rendered with the wrong font-size and your headers appear bold.

To avoid all these things you can work your way through all the little errors and add them into the additional stylesheet like this:

/* stylesheet debug */
font, b, i{
	display: block; /* make it stand out on the page*/
	border:2px solid red; /* here is your problem !! */
	font-size: 8px; /* very small unreadable print*/
} 

h2 strong {
	font-weight: normal;
	display: block; /* make it stand out on the page*/
	border:2px solid orange; /* orange means: not nice, but is ok */
}  

Don’t want DIV elements inside the #content area, because they should use paragraph element?

#content div {
	display: block; /* make it stand out on the page*/
	border:2px solid red; /* here is your problem !! */
	font-size: 8px; /* very small unreadable print*/
}

These are simple examples but they show how the idea works. Once you know your template and what can happen when things go wrong, you can dig a little deeper and apply strict rules to where an element can and is allowed to appear.

For example, I have seen some editors use BLOCKQUOTE to indent paragraphs. This is a common mistake and will not be allowed, so I added this to the rules:

.productpage #content blockquote{ /* error markup in here */ } 

I am not allowing BLOCKQUOTE elements in the content block on a productpage. (note: You need to be able to distinguish different page types from each other).

When should you show these errors?

Obviously, we shouldn’t show these errors to the visitors. That is where the need to detect the login comes in. ALl you have to do is test if the person viewing the website is logged in as an author and based on that test, add the debug stylesheet or not.

This is the XSLT code I am using in Umbraco:

<?xml version="1.0"?>
	<xsl:stylesheet version="1.0"
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		xmlns:msxml="urn:schemas-microsoft-com:xslt"
		xmlns:umbraco.library="urn:umbraco.library"
		exclude-result-prefixes="msxml umbraco.library">
	<xsl:output method="html"/> 

	<xsl:param name="currentPage"/> 

	<xsl:template match="/">
		<xsl:if test="umbraco.library:RequestCookies('UserContext') != ''">
			<link media="all" href="/css/debug.css" id="debug" rel="stylesheet" type="text/css"/>
		</xsl:if>
	</xsl:template>
</xsl:stylesheet>

This is an XSLT macro, using the default Umbraco library to detect the cookie with the authentication flag. If it is found and not empty, add the debug stylesheet in the HTML header of the page.

The next step is to determine if you are going show the errors in a life environment to make sure editors have to care about it, or that you will leave it out and only use it for your quality review.

I always show the big errors and surround them with a red border. Using font tags to override my CSS rules is unacceptable so these will be shown in the life environment.

All you have to do is split the severe errors from the others and put them in a separate sylesheet. Then hardcode the reference to the stylesheet into the template. That way the severe errors will always be shown and editors will have to check their pages in preview mode before actually publishing.

I am not sure if this technique is useful for other webmasters, but if you do plan to use, let me know.

I am currently using it on my Umbraco installation and in the beginning it cause a lot of support because the editors will call you. After a while they will know and they have learned to use standard based markup. After that support calls will go down again.

Webpatterns: HTML & CSS working together

John Allssop wrote an article about using webpatterns in HTML and he wondered if we could gather these patterns and put them on a website. He started a blog and a wiki on webpatterns.org to document webpatterns. It is not active yet but I really think you should keep an eye on it. He is going to start a quizz similar to the “SimpleQuizz from Dan Cederholm”.

In the article, John talks about the use of classes and id’s but I doubt we can restrict webpatterns to HTML only. Most of the webpatterns that exist on the web today come from problems when using HTML together with CSS. And from that perspective, the relation between HTML and CSS is similar to the relation between XML and XSLT.

I previously wrote an article about the XML/XSLT combo. When to choose between pointing to an element or including an element. This relates to the use of attributes or elements. When to point or include depends on what you want to achieve with the XSLT transformation.

In many cases you can improve your XML if you think about the XSLT transformation that will happen later on. This same thinking can improve your HTML and CSS code.

Pointing in HTML and CSS is the same as using an id or class attribute and use an id selector for a rule in your style sheet.

HTML CSS

#navBar{ declaration in here}
# content { declaration in here}

<div id="navBar&gt";</div>
<div id="content"></div>

XML/XSLT

<xsl:value-of select="element[@id=" navBar"]"/>
<xsl:value-of select="element[@id=" content"]"/>

<element id="navBar"/>
< element id="content"/>

In this case you can use an id to point to the element in question.

Now if you have nested elements and you include the element you want to transform or style, you get something like this.

HTML CSS

div#contact ul li, div li {declaration in here}

XML/XSLT

<div class=”contact”> <ul> <li> … 

<contact><person><lastName> …. <xsl:value-of select=”contact/person/lastName”/>
or
<xsl:Value-of select=”contact//lastName”/>

We can call the first example redundant because using an id and pointing to each element will result in a large stylesheet with lots of repetition.

The second example is using dependency because your are depending on relationships between elements.

The article “Dependency versus redundancy”on mezzoblue clearly documents the difference between the 2 patterns. Both directions, coding for redundancy or dependency, have their own advantages and in many cases your are going to use both.

To come back to the pattern subject, the pattern would tell you when to choose for redundany or dependency. When to use an id or notand rely on child parent relations.

And I think it is clear that this relates to both CSS and HTML.

To close this post, I am really expecting to see big things happening on the webpattern.org website but only if we provide feedback and tell John if this or that pattern actually works.

So go and have a look and subscribe to his feed.

XML into PHP array using XSLT

Don’t want to read everything? Jump to the code or leave a comment

9 months ago I wrote a few lines PHP-code to get XML data into an array. The reason why I wrote this is hard to recall but in the process I had to solve a problem: how do I get a serie of values from an XML file into a PHP array?

Lots of solutions exist like the package PEAR::XML_Serializer but they were all too advanced for what I needed.
All I needed was a simple function or class that would do just that: convert xml file with values to php array. And I found a simple trick using nothing more than XSLT.

If you look at an array in PHP, the data inside is a storable presentation of a value. To get the value back into a string you have to call the serialize() function. Getting the array back from the string, you guessed it, call the unserialize() function. I’m not getting any deeper into this, the manual entries are very clear and the array section of the PHP manual is very good.

Once you know how a serialized array looks like, you can generate it with whatever you have. You can generate it through an XSLT transformation, like I did.

(more…)

XML documenten opbouwen: Element versus Attribuut

Uche Ogbuji, gastspreker op XML Europe 2004, geeft
enkele basisprincipes om XML documenten op te bouwen. Het belangrijkste principe dat in zijn artikel naar voren komt is dat je attributen in XML documenten enkel gebruikt om machine-gegevens op te slaan, en elementen om leesbare gegevens weer te geven.

Atributen zoals een ID, zijn referentiepunten voor programma’s. Ze worden gebruikt bij het transformeren van documenten d.m.v. XSLT en komen zelden voor in de output van een transformatie. De inhoud van een element daarentegen bevat leesbare gegevens zoals een naam, de titel van een boek
enz., die getoond worden aan de gebruiker.
Een ID kan soms rare vormen aannemen, bvb. “sessionid{az12sqdb54qdg54qfb}”, waardoor de leesbaarheid van het document vermindert. Zet je de ID in een attribuut, dan wordt het duidelijk dat dit gegeven niet relevant is voor de lezer.

Machine-gegevens in een attribuut plaatsen heeft nog meer voordelen. XSLT heeft bvb. meer mogelijkheden om attributen te bewerken. Een element uit de tree halen is veel eenvoudiger als je een attribuut-waarde hebt omdat je een ID rechtstreeks in een XPath expression kan plaatsen.
Onderstaande XSL voorbeeld heeft de variabele “username” ingesteld en gaat op zoek naar de auteur met de username waarde “jpeters”.

<xsl :variable name="username">jpeters</xsl>
<xsl :value-of select="//name[@username = $username]">
</xsl>

Het XML document gaat als volgt:

< ?xml version="1.0"?>
  <authors>
<author>
<name username="jpeters" pw="myPWencrypted">
JanPeters
</name>
</author>
</authors>

Het resultaat is de inhoud van het element “name” waarmee de usernaam “jpeters” overeenkomt. Nu nog het wachtwoord opnemen als attribuut en je hebt zo een authenticatie systeem.

Categories

About

My name is Len Dierickx and this is my personal blog. I studied Musicology at the UG, long time ago but got more and more into webdevelopment. I started this blog because the EuroIA summit in Brussels (Belgium, Oct 2005), was such an inspiration. And I was thinking about a blog on IA a while now, so that was the extra kick I needed to get it actually done.

Powered by WordPress