Here is a very simple example of using alf:parseXMLDocuments with both XSL and Freemarker. This example will effectivley load all XML output files from the root dir of type ‘blog-article’ and complie them into an output rendition.
Download the example files here or
Web Form – blog-article.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:pr="http://www.alfresco.org/alfresco/pr"
targetNamespace="http://www.alfresco.org/alfresco/pr"
elementFormDefault="qualified">
<!-- Blog Article categories -->
<xs:simpleType name="category">
<xs:restriction base="xs:normalizedString">
<xs:enumeration value="Product"/>
<xs:enumeration value="Web 2.0"/>
<xs:enumeration value="Internet"/>
<xs:enumeration value="Event"/>
<xs:enumeration value="Training"/>
</xs:restriction>
</xs:simpleType>
<!-- Blog Article fields -->
<xs:element name="blog_article">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:normalizedString"/>
<xs:element name="abstract" type="xs:normalizedString"/>
<xs:element name="body" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
<xs:element name="keywords" type="xs:normalizedString" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="category" type="pr:category" default="Internet"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XSL Example – blog-article.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:pr="http://www.alfresco.org/alfresco/pr">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="all_press_releases" select="alf:parseXMLDocuments('blog-article', '')"/>
<xsl:for-each select="$all_press_releases">
<h2><xsl:value-of select="pr:title"/></h2>
<h3><xsl:value-of select="pr:abstract"/></h3>
<p><xsl:value-of select="pr:abstract"/></p>
<br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Freemarker Example – blog-article.html.ftl
<#ftl ns_prefixes={"D":"http://www.alfresco.org/alfresco/pr", "pr":"http://www.alfresco.org/alfresco/pr"}>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<#assign docs = alf.parseXMLDocuments('blog-article', '/')>
<#list docs as x>
<h2><a href="#">${x.title}</a></h2>
<h3>${x.abstract}</h3>
<p>${x.body}</p>
<br />
</#list>
</body>
</html>
{ 10 comments… read them below or add one }
Fantastic! Why can’t they have information like this on the Alfresco site? The wiki is a mess and completely useless.
The article is fantastic for giving a sample of using this technology, but I am having issues actually testing out, I keep getting XPath errors when trying to enter a sample blog. The XPath errors come from the xsl file, I was wondering if you could give me a sample configuration of the form you build to use this through alfresco.
Hi Roger,
There is a form example in the post and download file. Just install the blog-article.xsd file. I have just created a new web project, installed the XSD and both rendition templates just giving them a default rendition path, and the files are produced as expected.
Regards, Ben.
Hi,
I have a XML file with an specific schema and I want to apply a presentation template based in XSLT (not Freemarker)
I have copied the XSLT file into Presentation Templates of Alfresco, but when I select the template it is not correctly viewed.
It’s like transformation wasn’t applied.
Greetings
Hi Pablo,
This is kind of off topic however you need to add the web form (XSD schema) and associate the XSLT (or Freemarker) template under the “Edit Web Project” section. This gives you the option to “add a web form” and associate a template.
For a step by step guide along with some examples, take a look at WCM 2_1 Product Evaluation Guide.pdf from the Content Community – http://www.alfresco.com/community/register/ Its a little dated but is very useful.
Ben.
A question to the author:
Can you add inside XSL and Freemarker templete an example of alf:file_name usage ?
Alfresco wiki, is not very exhaustive, and contains some errors. (http://wiki.alfresco.com/wiki/WCM_Forms_Rendering)
Cheers
Hi Giorgio,
Take a look at Yongs blog. He is writing a series of tutorials on Alfresco Web Forms. He has an example of alf:file_name usage – http://drquyong.com/myblog/?p=65
Best regards,
Ben.
Thank you Ben,
I checked Youngs blog, but he is using XSLT.
I always use XSL templates but now my output is not HTML but an RSS feed and my choice was to use Freemarker.
Is there a way to achive the some result using Freemarker too ?
I checked once again Youngs blog.
He is not using alf:file_name (this is used to retrieve the name of the XML file. The name which users type inside the web form when they create a new document or page)
If you check part3 (http://drquyong.com/myblog/?p=96) of Youngs article you can see that he is retrieving the name of an image loaded using a file picker. This is quite easy and is not inherent with my question.
Eventually I got the answer to my question, I will publish it on your blog ’cause I think will be useful for everybody to know.
It’s a tricky way, but it works.
We will have get using Freemarker the alf.filename attribute in lower case and removing the final .xml
Here is the code:
<#list parsedPost.@@ as attr> <#-- Retrieve Filename - this made me sweat - Giorgio Cardellini -->
<#if attr?node_name = 'file_name'>
<#list attr?split(".") as name>
<#if name_has_next>
<#assign myFilename = name?lower_case>
</#if>
</#list>
</#if>
</#list>
${myFilename} this will print out the wanted result and can be used insed URL to have a friendly navigation
Have a nice day !