Passing variables to XSLT

2020-06-22 18:03发布

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">

  <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.

标签: php xml xslt
4条回答
家丑人穷心不美
2楼-- · 2020-06-22 18:18

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.

查看更多
三岁会撩人
3楼-- · 2020-06-22 18:24

There are a few issues here stopping it from working, some mentioned already..

The namespace as stated above:

<stylesheet xmlns:php="http://php.net/xsl"

The for-each...I tend to use:

<xsl:template match="/">

then

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

Also, the quotes wasn't correct, as above, the id doesn't need the ''

With those three changes it works....

查看更多
beautiful°
4楼-- · 2020-06-22 18:26

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

查看更多
你好瞎i
5楼-- · 2020-06-22 18:39

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"
>
查看更多
登录 后发表回答