【问题标题】:Why can't PHP print xml code with Dublin Core tags? [duplicate]为什么 PHP 不能使用 Dublin Core 标签打印 xml 代码? [复制]
【发布时间】:2018-01-05 18:38:58
【问题描述】:

我尝试用这个 php 函数打印文件 affresco.xml 的内容:

<?php $xml=simplexml_load_file("http://localhost/chiesa/affresco.xml") or 
die("Error: Cannot create object");
echo $xml->title . "<br>";
echo $xml->description . "<br>";
?>

但它没有打印任何东西。在我尝试了相同的功能但我从 xml 标签中删除了“dc”之后,它就起作用了。这是 affresco.xml:

<?xml version="1.0"?>

<metadata
  xmlns="http://localhost/chiesa/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://localhost/chiesa/  
  http://localhost/chiesa/schema.xsd"
  xmlns:dc="http://purl.org/dc/elements/1.1/">

  <title>
    UKOLN
  </title>
  <dc:description>
    UKOLN is a national focus of expertise in digital information
    management. It provides policy, research and awareness services
    to the UK library, information and cultural heritage communities.
   UKOLN is based at the University of Bath.
  </dc:description>


</metadata>

如何在不删除“dc”标签的情况下打印?

【问题讨论】:

  • dc 是 xml 文件中的命名空间。
  • 感谢您的回答。我知道“dc”是一个命名空间,但我不明白如何使用我的 php 函数从 xml 打印值。

标签: php xml xsd


【解决方案1】:

要获得描述,您需要在 'dc' 命名空间中找到子项:

$xml=simplexml_load_file("http://localhost/chiesa/affresco.xml") or die("Error: Cannot create object");
$ns = $xml->getNamespaces(true);
echo $xml->title;
echo $xml->children($ns['dc'])->description;

demo

【讨论】:

    猜你喜欢
    • 2020-09-27
    • 2016-12-20
    • 2019-08-02
    • 2012-03-23
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 2016-12-31
    相关资源
    最近更新 更多