2009-05-14 20 views
4

私はXML/XSLが初めてです。ルール文字列にvarを渡して正しいデータを返すことができるようにしたい。XSLTに変数を渡す

今、私はこの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); 
?> 

私のxmlファイルは次のようになります。私がテストしたとき

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

そして、私のXSLは、この

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

のように見えますこのようなURL:

http://foo.com/sanbox/index.php?id=2

私は取得のみ:XPath式で

 
Profile 
User Profiles ID Name Date of Birth. 
+0

....それが動作するこれらの3回の変更では ''

を必要としない、正しくありませんでした'XSLTProcessor :: registerPHPFunctions()'はあまり文書化されていません... http://en.wikibooks.org/wiki/PHP_Programming/XSL/registerPHPFunctionsを参照してください –

答えて

3

を、それは読ん必要がありますので、変数名$ IDの周りの任意のqoutesがあってはならない。

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

また、あなたが<xsl:value-of select="$id"/>を入れることができますその値が確実に渡されるようにします。

1

ねえ、私はまた、XSL/XMLの実装に新しいですが、私は少しのためにあなたのコードで遊ん、私はそれが動作することができませんでしたが、あなたは

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

を変更した場合でも

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

正しい情報を取得するにもかかわらず、厄介なエラーが表示されます。もしあなたが数字だけでなく ""を削除した場合、それはより多くのエラーを与えます。私はこれを行う別の方法を見つけましたが、私はそれをテストする時間がありませんでした。 Client Side XSLT

私はあなたと同じ問題を抱えており、この問題を解決したいと思っています。

警告:XSLTProcessor :: transformToXml()[xsltprocessor.transformtoxml]:xmlXPathCompOpEval:Eにおける未定義のプレフィックスのphpにバインドされた関数function:\ xampplite \ htdocsに\ XSL \ index.phpのライン上の17

警告:XSLTProcessor :: transformToXml()[xsltprocessor.transformtoxml]:xmlXPathCompiledEval:1つのオブジェクトがスタックに残っています。 Eで:\ xampplite \ htdocsに\ XSL \ index.phpのライン上の17

警告:XSLTProcessor :: transformToXml()[xsltprocessor.transformtoxml]:xmlXPathCompOpEval:Eにおける未定義のプレフィックスのphpにバインドされた関数function:\ xampplite \ htdocsに\ XSL \ index.php on line 17

警告:XSLTProcessor :: transformToXml()[xsltprocessor.transformtoxml]:xmlXPathCompiledEval:2つのオブジェクトがスタックに残っています。 Eで:\ xampplite \ htdocsに\ XSL \ index.phpのライン上の17

プロフィール

ユーザープロファイル誕生 2マークアントニーの ID名日付79900200

7

あなたが含まれてhaventため、エラーがあります正しい名前空間。

あなたのXSLでは

:スタイルシートの宣言の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" 
> 
+0

この小さな人生は安全でした、ありがとうございました) – MackieeE

+0

'XSLTProcessor: :registerPHPFunctions() 'の記述が不十分です。http://en.wikibooks.org/wiki/PHを参照してください。詳細については、P_Programming/XSL/registerPHPFunctionsを参照してください。 –

0

いくつかは、すでに述べたように、作業からそれを停止し、ここでいくつかの問題があります。..

を上述したように

名前空間:ため、各

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

ザ・...私が使用する傾向がある:

<xsl:template match="/"> 

その後、

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

はまた、引用符は、上記のように、idは