XML: setting values of namespace elements

孤者浪人 提交于 2019-12-13 04:31:53

问题


Continuing my previous question: simplexml_load_file does not recognize <dc:title> tags

How would you go about setting a value for a <dc: element? I managed to print its value but I read online that the XML parser is only for selecting data, not setting it.

EDIT: Using simplexml_load_file() and PHP

What i'm trying to do using PHP basically is to change this <dc:title>test</dc:title> to <dc:title>HELLO</dc:title> for example, and if that tag <dc:title> doesn't exist than add it.


回答1:


a <dc: element?

As noted in the first answer to the linked question: dc is not an element name. It is a namespace name.

To add an element that is in a namespace use the applicable method (maybe an overload) to specify the namespace. However this depends on the parser you are using (and you don't specify).

Eg. with .NET's LINQ to XML you create an instance of XNamespace from the namespace's URI and then, as it overloads the + operator, you prepend it to the element name:

var ns = new XNamespace(namespaceUri);
var newElement = doc.firstNode.Add(new XElement(ns + "ElementName"));


来源:https://stackoverflow.com/questions/30890764/xml-setting-values-of-namespace-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!