On 22 Oct 2009, at 16:39, Peter Ford wrote:
Matthew Croud wrote:
Hi Guys,
Well i;ve been slaving on with my PHP XML endeavors and i'm loving
it,
just finishing the meaty parts of my XSLT for dummies book too.
I have a question which asks "is it possible?".
Using XSLT I can collect specific parts of my XML using something
sexy
like <xsl:template match="umbungo">. Lets say however, that I need
to
use XSLT, but I would want the user to select which element they
request. In other words, they are given a form with options and that
form can manipulate the .XSL file.
Now I know it could be done in a lengthly manner by just opening
the XSL
file and manipulating it like fopen or something like that, but is
there
a way to somehow embed the contents of the xml into the php code
(like
using <<< EOF for html), and being able to substitute the template
match
string for a variable ?
Any ideas ?
Thanks Gamesmaster,
Matt
A bit off-topic (since XSLT is not PHP...) but here goes.
First I need to clarify what you are doing -
<xsl:template match="umbongo"> ... </xsl:template>
defines the template portion, and that is pretty immutable.
At some point you must have in your XSLT something like
<xsl:apply-templates select="umbongo"/>
This is the bit you can play with:
so:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform
">
<xsl:output method="xml" encoding="UTF-8" /> <!-- or whatever -->
<!-- Pass in a parameter to the XSL to define which thing we want to
match -->
<xsl:parameter name='matchMe'/>
<!-- Define templates for all the things we might want to match -->
<xsl:template match='umbongo'><xsl:apply-templates/></xsl:template>
<xsl:template match='fivealive'><xsl:apply-templates/></
xsl:template>
<!-- etc. -->
<!-- now the clever bit -->
<xsl:template match='/'>
<xsl:apply-templates select='//*[name()=$matchMe]'/>
</xsl:template>
</xsl:stylesheet>
Essentially you need to apply the template of any element, but only
those whose
name matches your request.
Note that
<xsl:apply-templates select='$matchMe]'/>
doesn't work... :(
So now if your PHP does something like
$xslDom = new DOMDocument();
$xslDom->load('matchMe.xsl');
$xslt = new XSLTProcessor();
$xslt->importStylesheet($xslDom);
$xslt->setParameter('','matchMe','umbongo');
$xmlDom = new DOMDocument();
$xmlDom->load('some_document_that_has_an_umbongo_tag.xml');
echo $xslt->transformToXML($xmlDom);
you should get the results of the 'umbongo' template (only)
'f course, this is not tested, but I have used this idea in working
code
--
Peter Ford phone: 01580 893333
Developer fax: 01580 893399
Justcroft International Ltd., Staplehurst, Kent
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Utter brilliance, this is superb, I might drop you a mail to clarify a
few things later Peter,
but your reply was superb and has given me many options to try out.
Thanks again!
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php