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.

2
  • Why did you tag your question xslt? Do you want an answer using XSLT? Commented Jun 17, 2015 at 14:55
  • Because I thought there might be a solution using XSLT, I have no experience with XSLT. I'd like to have the solution as simple as possible. Commented Jun 17, 2015 at 15:03

1 Answer 1

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"));
Sign up to request clarification or add additional context in comments.

5 Comments

I've used simplexml_load_file() and PHP, please review my revised question.
@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.
simplexml_load_file Interprets an XML file into an object, the problem is that a <dc:title> 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 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 or, using simplexml_load_file. 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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.