Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 2nd, 2014, 02:45 PM
Registered User
 
Join Date: Jan 2014
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default An approach to transform XML to HTML in a modular manner

Hi there

I am skilled at XSLT (1.0 mostly), XML and PHP, but my current project requires that I implement an XML to HTML transformation. This is new to me and I am currently checking out my options.

One of them being that I use DOMDocument and XSLT stylesheets to transform the XML file to HTML output.

I have the following code that works fine

Code:
<?php
       $stylesheet = '<?xml version="1.0" encoding="iso-8859-1"?>
		<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
		<xsl:template match="/">
 			<html><head></head><body>
				<xsl:for-each select="//nodes/*">
  					<xsl:value-of select="name()"/><br/>
				</xsl:for-each>
			</body></html>
		</xsl:template>
		</xsl:stylesheet>';
	
   	$xslDoc = new DOMDocument();
	$xslDoc->loadXML( $stylesheet );

   	$xmlDoc = new DOMDocument();
   	$xmlDoc->load("test.xml");

   	$proc = new XSLTProcessor();
   	$proc->importStylesheet($xslDoc);
   	echo $proc->transformToXML($xmlDoc);
Because I have a modular approach in mind, i.e. the users should be able to select which parts of the XML file they want to be transformed and displayed, and the function 'XSLTProcessor::importStylesheet' does not allow to import more than one stylesheet file, I decided to create a stylesheet string instead. The stylesheet string will be created according to the user selection (not yet in the code).

The code loads then the final stylesheet string with the 'DOMDocument::loadXML' function.

My questions:

1. If you load an XML string, you have to wrap it in an XML node. Otherwise, it does not seem to be a real XML document. Do I have to do this with the XSLT string too?
2. Does anyone has experience with loading XSLT strings? Do I have to worry that some tags/characters will be filtered, if I use a string?
3. I would also appreciate comments on whether this is a good approach or not. Constructive comments and suggestions are most welcome.

My system:

libxslt Version 1.1.28, libxslt compiled against libxml Version 2.8.0, EXSLT enabled, libexslt Version 1.1.28, libxml2 Version 2.8.0
PHP 5.4.10 on my localhost webserver MAMP

regards goedda
 
Old January 3rd, 2014, 06:13 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

As for 1, any string you want to load respectively parse successfully with the loadXML method has to be a well-formed XML document and any such document needs a single root element containing all other elements. And of course any XSLT stylesheet needs to have a "xsl: stylesheet" respectively "xsl: transform" as its root element.

As for 2, there shouldn't be any difference between loading an XSLT stylesheet from a file with the "load" method or loading an XSLT stylesheet as a string with the "loadXML" method, as far as execution of the stylesheet is concerned.

As for 3, while constructing XSLT code as run-time as a string is possible, I wonder whether you need that. A more robust solution in my view would be to try to define parameters in your stylesheet that could be set to values a user passes in e.g.

Code:
<xsl:param name="p1"/>

<xsl:template match="/">
  ...
  <xsl:apply-templates select="//foo[contains(bar, $p1)]"/>
  ...
</xsl:template>
If you really think you need to construct the XSLT code at run-time then I think, as with any XML to be constructed, a more robust solution than string concatenation is to use an XML API like XmlWriter or DOM or even XSLT to construct the XML as that way you don't have to worry about the XML syntax (like escaping ampersand or less-than signs as needed).
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old January 3rd, 2014, 08:51 AM
Registered User
 
Join Date: Jan 2014
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your quick reply. It was what I was looking for.

1. The XML document has a single root element called <storage>...</storage>. And the stylesheet I used, you can see in my previous post, the stylesheet declaration is also given.

2. I haven't noticed a difference either, but I am just at the very beginning of my project. And still evaluating my options...

3. Oh, that for I will need some time parse. Your suggestions certainly sound interesting, but some of it is not known to me.

Just for clarification: The XML file is given, there is nothing I can do about that one. All I can do is choose what pieces and how to transform the raw XML data into a human readable form (HTML page) and possibly later into a PDF printout.

I know that some guys think of XSLT transforming an XML document into another XML document. Maybe you meant something like that and I just misunderstood...





Similar Threads
Thread Thread Starter Forum Replies Last Post
xml flat file transform to nested tag xml using xslt transform adsingh XSLT 0 August 7th, 2013 01:27 PM
need help with this html to xml transform sahori79 XSLT 4 August 13th, 2009 07:07 AM
Will this be a good approach with XML? mike_remember ASP.NET 1.0 and 1.1 Professional 0 October 23rd, 2007 09:11 AM
transform a xml to a html table robert_trudel_fr XSLT 3 December 3rd, 2006 02:16 PM
VS.NET MODULAR APPROACH 3Moose BOOK: ASP.NET Website Programming Problem-Design-Solution 2 January 16th, 2005 12:20 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.