草庐IT

php - 将 PHP DOM 传递给 XSLT

coder 2024-07-02 原文

我正在尝试在 PHP 文档中加载 XML 并将其传递给 XSLT。我能够将 XML 文件直接加载到 XSLT 中,但我试图避免使用实际文件,而只是使用 cURL 来获取我的数据。

这是我的 PHP 脚本:

<?php

header("Content-type: text/xml");

$ch = curl_init("domain1.com/sample1.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);       
curl_close($ch);
$sample1 = new DOMDocument();
$sample1->formatOutput = true;
$sample1->loadXML($output);
$sample1->save("sample1.xml");

$ch = curl_init("domain1.com/sample2.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);       
curl_close($ch);
$sample2 = new DOMDocument();
$sample2->formatOutput = true;
$sample2->loadXML($output);
$sample2->save("sample2.xml");

/* load the xml file and stylesheet as domdocuments */
$xsl = new DomDocument();
$xsl->load("transform.xsl");

/* create the processor and import the stylesheet */
$proc = new XsltProcessor();
$xsl = $proc->importStylesheet($xsl);

$mergedxml = new DomDocument();
$mergedxml->loadXML($proc->transformToXML($sample1));
echo $mergedxml->saveXML();
$mergedxml->save("results.xml");

?>

这是我的 XSLT 文件中使用第二个文件 (sample2.xml) 的行:

<xsl:variable name="input2" select="document('sample2.xml')/DATA"/>

我知道您可以在 XLST 中调用一些 PHP 函数,但我似乎无法弄清楚如何在不先保存到文件的情况下将 XML DOMDocument 直接传递给 XSLT。这可能吗?或者我需要以不同的方式执行此操作吗?我是 PHP 和 XSLT 的新手,所以非常感谢您的帮助。

谢谢。

最佳答案

这是一个可能的解决方案 - 它不会适用于所有情况,例如当您需要进行 POST 来检索 XML 时,但是它可以避免必须保存任何 XML 文件。让 XSLT 处理器为您执行检索,您可以使用处理器上的 setParameter 方法指定文件。

<?php

/* load the xsl file into a DOMDocument, and set the documentURI  */
$xsl = new DomDocument();
$xsl->load("transform.xsl");
$xsl->documentURI = "/path/to/transform.xsl";

/* need a document, can be empty */
$doc = new DOMDocument();

/* define the sources for your external XML, can be static or dynamic */
$params = array(
    'source-books' => 'http://example.com/books.php',
    'source-films' => 'http://example.com/films.xml',
);

/* create the processor and import the stylesheet, set your source parameters */
$proc = new XsltProcessor();
$proc->importStylesheet($xsl);
$proc->setParameter('',$params);

echo $proc->transformToXml($doc);

在 XSL 中,如果合适,您可以为示例定义默认源,它可以是本地文件系统或 URI 中的相对路径。

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
  <xsl:output method="html" omit-xml-declaration="yes" />

  <!-- define default parameters -->
  <xsl:param name="source-actors" select="'actors.xml'"/>
  <xsl:param name="source-books" select="'http://example2/com/books.xml'"/>

  <!-- pickup the source parameters -->
  <xsl:variable name="films" select="document($source-films)/films"/>
  <xsl:variable name="books" select="document($source-books)/books"/>
  <xsl:variable name="actors" select="document($source-actors)/actors"/>

  <xsl:template match="/">
    <html>
      <body>
        <h4>
            Found <xsl:value-of select="count($films/film)" /> films in document at <xsl:value-of select="$source-films" />
        </h4>
        <ul>
            <xsl:apply-templates select="$films/film" />
        </ul>
        <h4>
            Found <xsl:value-of select="count($books/book)" /> books in document at <xsl:value-of select="$source-books" />
        </h4>
        <ul>
            <xsl:apply-templates select="$books/book" />
        </ul>
        <h4>
            Found <xsl:value-of select="count($actors/actor)" /> actors in document at <xsl:value-of select="$source-actors" />
        </h4>
        <ul>
            <xsl:apply-templates select="$actors/actor" />
        </ul>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="film|book|actor">
      <li><xsl:value-of select="@uid" />: <xsl:value-of select="." /></li>
  </xsl:template>
</xsl:stylesheet>

关于php - 将 PHP DOM 传递给 XSLT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6902000/

有关php - 将 PHP DOM 传递给 XSLT的更多相关文章

  1. ruby - rails 3 redirect_to 将参数传递给命名路由 - 2

    我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use

  2. ruby-on-rails - 如何生成传递一些自定义参数的 `link_to` URL? - 2

    我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些

  3. ruby - 在 Ruby 中按名称传递函数 - 2

    如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只

  4. ruby - 如何将 Puma::Configuration 传递给 Sinatra? - 2

    这是我的网络应用:classFront我是这样开始的(请不要建议使用Rack):Front.start!这是我的Puma配置对象,我不知道如何传递给它:require'puma/configuration'Puma::Configuration.new({log_requests:true,debug:true})说真的,怎么样? 最佳答案 配置与您运行的方式紧密相关puma服务器。运行的标准方式puma-pumaCLI命令。为了配置puma配置文件config/puma.rb或config/puma/.rb应该提供(参见examp

  5. jquery - 如何将 AJAX 变量从 jQuery 传递到他们的 Controller ? - 2

    我有一个电子邮件表格。但是我正在制作一个测试电子邮件表单,用户可以在其中添加一个唯一的电子邮件,并让电子邮件测试将其发送到该特定电子邮件。为了简单起见,我决定让测试电子邮件通过ajax执行,并将整个内容粘贴到另一个电子邮件表单中。我不知道如何将变量从我的HAML发送到我的Controllernew.html.haml-form_tagadmin_email_blast_pathdoSubject%br=text_field_tag'subject',:class=>"mass_email_subject"%brBody%br=text_area_tag'message','',:nam

  6. ruby - 如何将 lambda 传递给 Hash.each? - 2

    如何将lambda传递给hash.each,以便我可以重复使用一些代码?>h={a:'b'}>h.eachdo|key,value|end=>{:a=>"b"}>test=lambdado|key,value|puts"#{key}=#{value}"end>test.call('a','b')a=b>h.each&testArgumentError:wrongnumberofarguments(1for2)from(irb):1:in`blockinirb_binding'from(irb):5:in`each'from(irb):5from/Users/jstillwell/.rv

  7. ruby - 将命令行上的变量传递给 Cucumber 测试 - 2

    我正在尝试将cucumber项目的用户名和密码置于版本控制之外。有没有办法在命令行上手动将用户名和密码等变量传递给Cucumber脚本?我的备份计划是将它们放在一个YML文件中,然后将该文件添加到gitignore,这样它们就不会被置于版本控制中。 最佳答案 所以,我看到了您对铁皮人的评论,答案是肯定的。cucumberPASSWORD=my_passwordPASSWORD被设置为环境变量,您可以通过将其引用为ENV['PASSWORD']来使用它的值。例如,browser.text_field(:id=>'pwd').setEN

  8. Ruby HERE-DOC方法参数传递 - 2

    我正在尝试将自定义方法与here-doc一起使用,并希望传递参数(没有业务案例,我只是想学习ruby​​)。在这种情况下有没有办法传递参数?这是我目前所拥有的。方法简单,效果很好。defmeth1self.upcaseendstr1=现在,我想让我们放弃一些参数并尝试不同的变体,irb给了我一个茫然的眼神。#variation1defmeth2( 最佳答案 我猜这就是您要尝试做的:defmeth2(str1,str2)str1.upcase+"..."+str2.downcaseendstr2=meth2("SOMESTRING\n

  9. ruby - 将运算符传递给函数? - 2

    也许这听起来很荒谬,但我想知道这对Ruby是否可行?基本上我有一个功能...defadda,bc=a+breturncend我希望能够将“+”或其他运算符(例如“-”)传递给函数,这样它就类似于...defsuma,b,operatorc=aoperatorbreturncend这可能吗? 最佳答案 两种可能性:以方法/算子名作为符号:defsuma,b,operatora.send(operator,b)endsum42,23,:+或者更通用的解决方案:采取一个block:defsuma,byielda,bendsum42,23,

  10. ruby - 在 ruby​​ 中将函数作为参数传递 - 2

    我正在努力研究ruby​​中的函数式编程,但似乎没有太多好的文档。本质上,我正在尝试编写一个具有Haskell类型签名的组合函数:[a]->[a]->(a->a->a)->[a]所以combine([1,2,3],[2,3,4],plus_func)=>[3,5,7]combine([1,2,3],[2,3,4],multiply_func)=>[2,6,12]等等我发现了一些关于使用zip和map的东西,但使用起来感觉真的很难看。什么是最“ruby”的实现方式? 最佳答案 嗯,您说过您了解zip和map,所以这可能没有帮助。但我会

随机推荐