在PHP中生成包含HTML的XML - 变量问题[关闭]

I am sitting on a little problem here:

I have a php file which generates xml data.

$requestXmlBody .= "<Version>$compatabilityLevel</Version>";

Now there are variables pulled from the upper php code and also HTML is generated

$requestXmlBody .= 
    '<Description>
        <![CDATA[
            <!DOCTYPE html>
            <html>
                <head>
                </head>
                <body>
                    <script type="text/javascript">
                        <!--some JS-->
                    </script>
                    <img src="http://www.myserver.com/pic.jpg" class="etalage_thumb_image" />
                </body>
            </html>
        ]]>
    </Description>';

Now strangely I cannot mix variables and HTML Code. As you can see I use CDATA for the HTML. I want to use a variable for the image name rather than a fixed link. So the code would look like this

$requestXmlBody .= '<Description>
    <![CDATA[
        <!DOCTYPE html>
        <html>
            <head>
            </head>
            <body>
                <script type="text/javascript">
                    <!--some JS-->
                </script>
                <img src="$imagelink" class="etalage_thumb_image" />
            </body>
        </html>
    ]]>
</Description>';

But this just does not work. I tried this

$requestXmlBody .= '<Description>
    <![CDATA[
        <!DOCTYPE html>
        <html>
            <head>
            </head>
            <body>
                <script type="text/javascript">
                    <!--some JS-->
                </script>
                <img src="]]>$imagelink<![CDATA[" class="etalage_thumb_image" />
            </body>
        </html>
]]>
</Description>';

But also this will not work. I even tried to hand over the php variable (which I grab from a session btw) to a JS variable and include it with document.write

Still no success.

This one would work

$requestXmlBody .= '<Description>
    $imagelink
</Description>';

But not together with the generated HTML code as you can see above.

Any help is appreciated.

Thanks

Separate concerns. Don't do several things at once. If you split your embedded HTML into its own variable, it gets much easier.

As soon as you have 'freed' the HTML string from the XML context, you'll see that the problem still exists. It is caused by quoting the string with single quotes, which prevent interpolation. You have to use string concatenation instead of embedding the variable directly.

$description = '<!DOCTYPE html>
    <html>
        <head>
        </head>
        <body>
            <script type="text/javascript">
                <!--some JS-->
            </script>
            <img src="' . $imagelink . '" class="etalage_thumb_image" />
        </body>
    </html>';

$requestXmlBody .= '<Description>
    <![CDATA[' . $description . ']]>
</Description>';

Be sure that your HTML string does not contain a CDATA section itself, since CDATA sections cannot be nested.

The best approach will be to use the writeCData method.

$link= 'link goes here';
$imagelink = '<img src="'.$link.'" /> ';

// serve xml doc as xml
header('Content-type: application/xml');

// set up the document
$xml = new XmlWriter();
$xml->openMemory();
$xml->startDocument('1.0', 'UTF-8');
$xml->startElement('description');

// CData output
$xml->writeCData($imagelink);
$xml->endElement();

// end the document and output
$xml->endElement();
echo $xml->outputMemory(true);