0

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.

Community
  • 1
  • 1
odedta
  • 2,430
  • 4
  • 24
  • 51

1 Answers1

0

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"));
Richard
  • 106,783
  • 21
  • 203
  • 265
  • I've used `simplexml_load_file()` and PHP, please review my revised question. – odedta Jun 17 '15 at 12:08
  • @odedta I know XML, and a little PHP, but not enough to quickly answer in PHP. Try reading the PHP docs for whatever `simplexml_load_file` returns. – Richard Jun 17 '15 at 15:55
  • simplexml_load_file `Interprets an XML file into an object`, the problem is that a `` is not a valid XML tag, it's a namespace and to select the values of those tags you need to register a namespace so the interpreter knows what they are, the problem is that xpath is only a query language and there are no functions for settings values, hence me asking about another solution. – odedta Jun 18 '15 at 05:19
  • @odedta I think you need to fix your understanding. `dc:title` is most definitely a valid XML element name ("title" in the namespace mapped to tag "dc"). If that PHP API provides no means to modify the returned object model, then perhaps you need to look at a different parser. Like [here](http://stackoverflow.com/q/23105465/67392) or, using [`simplexml_load_file`](http://stackoverflow.com/q/4748014/67392). Summary: 1. you misunderstand XML naming (the focus of my answer), 2. you have APIs and other options, 3 I do not have a solution to a problem that *this question* makes no mention of. – Richard Jun 18 '15 at 06:26
  • Yeah, I meant that it's not a regular tag in the sense that you need to define a namespace in order to view it in the output if you use `var_dump`. I will try the other parser you've mentioned because, AFAIK, `simplexml_load_file` is not the right one for this job. As for your last comment, I believe my question is simple and straightforward. – odedta Jun 18 '15 at 07:52