首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取exsl:node-set以在PHP中工作

获取exsl:node-set以在PHP中工作
EN

Stack Overflow用户
提问于 2010-06-01 20:48:33
回答 1查看 1.3K关注 0票数 9

我有以下PHP代码,但它不工作。我看不到任何错误,但也许我只是看不见。我在PHP 5.3.1上运行这个程序。

代码语言:javascript
复制
<?php
$xsl_string = <<<HEREDOC
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:exsl="http://exslt.org/common"
                extension-element-prefixes="exsl">
  <xsl:template match="/">
    <p>Hello world</p>
    <xsl:variable name="person">
      <firstname>Foo</firstname>
      <lastname>Bar</lastname>
      <email>test@example.com</email>
    </xsl:variable>
    <xsl:value-of select="exsl:node-set(\$person)/email"/>
  </xsl:template>
</xsl:stylesheet>
HEREDOC;

$xml_dom = new DOMDocument("1.0", "utf-8");
$xml_dom->appendChild($xml_dom->createElement("dummy"));

$xsl_dom = new DOMDocument();
$xsl_dom->loadXML($xsl_string);

$xsl_processor = new XSLTProcessor();
$xsl_processor->importStyleSheet($xsl_dom);
echo $xsl_processor->transformToXML($xml_dom);
?>

这段代码应该输出"Hello world“,后跟"test@example.com",但是电子邮件部分没有出现。知道出什么问题了吗?

-Geoffrey Lee

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-06-01 21:06:55

XSLT的问题在于提供的代码有一个默认的名称空间。

因此,<firstname><lastname><email>元素位于xhtml名称空间中。但是引用email时没有任何前缀:

exsl:节点集($person)/email

XPath认为所有无前缀的名称都在"no namespace“中。它试图查找位于"no namespace“中的名为emailexsl:node-set($person)的子项,但没有成功,因为它的email子项位于xhtml名称空间中。因此,没有选择和输出任何email节点。

解决方案

此转换:

代码语言:javascript
复制
<xsl:stylesheet version="1.0"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:x="http://www.w3.org/1999/xhtml"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:exsl="http://exslt.org/common"
  exclude-result-prefixes="exsl x">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="/">
    <html>
     <p>Hello world</p>
     <xsl:variable name="person">
      <firstname>Foo</firstname>
      <lastname>Bar</lastname>
      <email>test@example.com</email>
     </xsl:variable>
     <xsl:text>&#xA;</xsl:text>
     <xsl:value-of select="exsl:node-set($person)/x:email"/>
     <xsl:text>&#xA;</xsl:text>
    </html>
  </xsl:template>
</xsl:stylesheet>

在任何XML文档(未使用)上应用时,都会生成所需的结果

代码语言:javascript
复制
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:x="http://www.w3.org/1999/xhtml">
   <p>Hello world</p>
test@example.com
</html>

注意到

使用前缀x

  • The
  1. select attribute of <xsl:value-of>

更改添加的命名空间定义

exsl:node-set($person)/x:email

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2949836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档