tags:

views:

1087

answers:

5

I am new to XML/XSL. I want to be able to pass a var in a rule string and have that return the correct data.

Right now I have have this PHP:

<?php
$params = array('id' => $_GET['id']);

$xslDoc = new DOMDocument(); 
$xslDoc->load("test.xsl"); 

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

$xsltProcessor = new XSLTProcessor(); 
$xsltProcessor->registerPHPFunctions(); 
$xsltProcessor->importStyleSheet($xslDoc); 

foreach ($params as $key => $val)
    $xsltProcessor->setParameter('', $key, $val);

echo $xsltProcessor->transformToXML($xmlDoc);
?>

My xml file looks like this:

<Profiles> 
  <Profile> 
    <id>1</id> 
    <name>john doe</name> 
    <dob>188677800</dob> 
  </Profile> 
  <Profile> 
    <id>2</id> 
    <name>mark antony</name> 
    <dob>79900200</dob> 
  </Profile> 
  <Profile> 
    <id>3</id> 
    <name>neo anderson</name> 
    <dob>240431400</dob> 
  </Profile> 
  <Profile> 
    <id>4</id> 
    <name>mark twain</name> 
    <dob>340431400</dob> 
  </Profile> 
  <Profile> 
    <id>5</id> 
    <name>frank hardy</name> 
    <dob>390431400</dob> 
  </Profile> 
</Profiles>

And my xsl looks like this

<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

  <xsl:param name="id" />

  <xsl:template match="*">
    <html><body>
    <h2>Profile</h2>
    <table cellspacing="1" cellpadding="5" border="1"> 
      <caption>User Profiles</caption> 
      <tr><th>ID</th><th>Name</th><th>Date of Birth</th></tr> 

      <xsl:for-each select="/Profiles/Profile[id='$id']">
        <tr> 
          <td><xsl:value-of select="id"/></td> 
          <td><xsl:value-of select="php:function('ucwords', string(name))"/></td> 
          <td><xsl:value-of select="php:function('date', 'jS M, Y', number(dob))"/></td> 
        </tr> 
      </xsl:for-each> 
    </table> 
    </body></html>
  </xsl:template>
</xsl:stylesheet>

When I test the url like this:

http://foo.com/sanbox/index.php?id=2
I only get:
Profile
User Profiles ID    Name  Date of Birth.
+3  A: 

In the XPATH expression there should not be any qoutes around the variable name $id so it should read:

<xsl:for-each select="/Profiles/Profile[id=$id]">

Also, you could put a <xsl:value-of select="$id"/> in the document to make sure the value gets passed along.

PQW
+1  A: 

Hey, I'm also new to implementing xsl/xml but i played around with your code for a bit, I couldn't get it working but even if you change

<xsl:for-each select="/Profiles/Profile[id='$id']">

to

<xsl:for-each select="/Profiles/Profile[id='2']">

You still get a nasty error although it does fetch the right information. If you remove any "" or even the '' with anything but numeric it give more errors. I have found another way to do this although i haven't had time to test it. Client Side XSLT

I have the same problem as you and would really like to see this problem solved.

Warning: XSLTProcessor::transformToXml() [xsltprocessor.transformtoxml]: xmlXPathCompOpEval: function function bound to undefined prefix php in E:\xampplite\htdocs\XSL\index.php on line 17

Warning: XSLTProcessor::transformToXml() [xsltprocessor.transformtoxml]: xmlXPathCompiledEval: 1 objects left on the stack. in E:\xampplite\htdocs\XSL\index.php on line 17

Warning: XSLTProcessor::transformToXml() [xsltprocessor.transformtoxml]: xmlXPathCompOpEval: function function bound to undefined prefix php in E:\xampplite\htdocs\XSL\index.php on line 17

Warning: XSLTProcessor::transformToXml() [xsltprocessor.transformtoxml]: xmlXPathCompiledEval: 2 objects left on the stack. in E:\xampplite\htdocs\XSL\index.php on line 17

Profile

User Profiles ID Name Date of Birth 2 mark antony 79900200

fluf
A: 

This isn't the answer you want, but I found out the hard way recently that you can't just "pick up" XSLT. It is much more powerful and complicated than you think. You have to actually learn it before you use it, instead of diving in and doing a quick Google search to find the syntax.

The easiest thing to do is just use an XML document object and read the node information yourself with PHP and parse it yourself. Getting it right with XSLT means getting a good handle on XSLT first, so unless you have the time to study it, it's not going to happen.

I gave up on XSLT for the time being and did what I recommend above (albeit not in PHP). As a bonus, though, you can do all of your development in the language you already know rather than trying to put logic into both PHP and XSLT.

ShawnMilo
+2  A: 

The error is because you havent included the correct namespace.

In your xsl:stylesheet declaration include xmlns:php="http://php.net/xsl"

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:php="http://php.net/xsl"
>
PQW
A: 

I'm using PHP as web service in a Flex Project by Adobe Flash Builder. The solutions I've read are:

  • implementing functions as static
  • giving the correct namespace
  • ...

I tried all the solutions, but never succeeded.

Could any one solve this problem?

Baris Aydinoglu