Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the "$ns" and "$is_prefix" parameters about?

The SimpleXMLElement::__construct() method and the related functions simplexml_load_string() and simplexml_load_file() all have an optional pair of parameters related to XML Namepspaces: $ns and $is_prefix.

Despite I can see that those are related to XML namespaces I wonder what they are for and how they work.

like image 635
hakre Avatar asked Jun 23 '13 08:06

hakre


People also ask

What does an ns1 prefix mean on a URL?

Any time you see a URL address with an NS1 prefix, you're actually seeing the primary name server address. What Is an NS1 Prefix? | Techwalla Shop Gift Guides

What does the prefix NS mean in iOS?

Two-letter prefixes like NS and UI (for User Interface elements on iOS) are reserved for use by Apple. Show activity on this post. Basically NS comes from N ext S TEP, the original operating system that became Mac OS X when Apple acquired Next.

What is infix prefix and postfix in math?

Infix, Prefix and Postfix Expressions — Problem Solving with Algorithms and Data Structures 4.9. Infix, Prefix and Postfix Expressions ¶ When you write an arithmetic expression such as B * C, the form of the expression provides you with information so that you can interpret it correctly.

How to convert complex expressions to prefix and postfix notation?

So in order to convert an expression, no matter how complex, to either prefix or postfix notation, fully parenthesize the expression using the order of operations. Then move the enclosed operator to the position of either the left or the right parenthesis depending on whether you want prefix or postfix notation.


1 Answers

According to PHP manual, those two parameters have been added in PHP version 5.2. The official PHP 5 changelog does not note these changes explicitly but the PHP 5.2 update readme has these.

Then looking into the 5.2 source for the constructor (in lxr) it shows that this is related to the iterator:

sxe->iter.nsprefix = ns_len ? xmlStrdup((xmlChar *)ns) : NULL;
sxe->iter.isprefix = isprefix;

So I assume those two specify the XML Namespace that SimpleXMLElement will iterate over by default. A little test can verify this:

$xml = new SimpleXMLElement(
    '<root><a/><b/><c/></root>'
);

var_dump(count(iterator_to_array($xml))); #int(3)

By default, the iterator has three elements here: a, b and c. Now setting the parameters specifying the iteration to be over a different XML-Namespace than the default one changes this:

$xml = new SimpleXMLElement(
    '<root><a/><b/><c/></root>', 0, FALSE, "ns:1"
);

var_dump(count(iterator_to_array($xml))); #int(0)

The iteration now has zero elements because the root-element does not have any child-elements in the namespace of URI ns:1.

Changing the namespace of the root element to ns:1 will again reveal three elements because now those three child-elements are in that namespace, they inherit it from their parent:

$xml = new SimpleXMLElement(
    '<root xmlns="ns:1"><a/><b/><c/></root>', 0, FALSE, "ns:1"
);

var_dump(count(iterator_to_array($xml))); #int(3)

Same as if the children itself are in the namespace specified by that parameter pair and via a prefix on these elements:

$xml = new SimpleXMLElement(
    '<root xmlns:n="ns:1"><n:a/><n:b/><n:c/></root>', 0, FALSE, "ns:1"
);

var_dump(count(iterator_to_array($xml))); #int(3)
like image 179
hakre Avatar answered Oct 19 '22 10:10

hakre