Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saw <?var type="string" ?> in an XML string, but what does this mean?

Tags:

xml

I recently saw an XML string containing <?var type="string" ?>. I would like to know if anyone has any idea what this means? It has something to do with PHP I imagine. Here is a contextual snippet.

<?xml version="1.0" encoding="UTF-8"?>
<node>
   <?var type="string" ?>
   <somenode>Value</somenode>
</node>

I can't seem to search google for <?var so maybe you guys can help me out.

like image 940
dqhendricks Avatar asked Oct 27 '11 23:10

dqhendricks


2 Answers

This is an XML processing instruction, and has nothing to do with PHP. (It isn't legal PHP syntax anyway.) The XML declaration you see at the beginning of the document is a special kind of PI.

I can't say much beyond that, though, as I've honestly never seen XML PIs used as XML PIs for anything in the wild, ever. The <? ?> delimiters are usually recognized by developers as PHP delimiters (with a short opening delimiter) instead.

Actually, as discussed in the comments, the full PHP delimiters <?php ?> can be considered a kind of processing instruction, even though they're used in a ton of other places besides XHTML/XML documents. One could even say that PHP was designed to be XHTML-compliant via processing instructions.

In fact, the following XHTML markup, with a snippet of PHP, actually validates!

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title></title>
  </head>
  <body>
    <p><?php echo 'Hello world!'; ?></p>
  </body>
</html>

However, it does not validate with short opening tags, as an XML PI must begin with <? followed by at least one name character. See the very first link to the spec for the PI grammar.

like image 157
BoltClock Avatar answered Oct 25 '22 07:10

BoltClock


This is whats in the zend 5-5 study guide. The question is "What is wrong with this XML document?" and the answer is "nothing"

I suppose the part is for php code but the repeated xml declaration seems weird so I put this xml in a file and opened it in my browser and the error:

This page contains error on line 7 at column 6: XML declaration allowed only at the start of the document. Below is a rendering of the page up to the first error.

Value

So why does the study guide say there's nothing wrong with the xml??

<?xml version="1.0" encoding="UTF-8"?>
<node>
    <?var type="string" ?>
    <leaf>Value</leaf>
</node>

<?xml version="1.0" encoding="UTF-8"?>
<node>
    <?var type="string" ?>
    <leaf>Value</leaf>
</node>
like image 22
Louie Cheung Avatar answered Oct 25 '22 06:10

Louie Cheung