Archive

Archive for December, 2008

Wrapping CDATA

December 26th, 2008 No comments

function clean_up_xml($xml_string='', $array_of_tags)
{
    if (count($array_of_tags) == 0)
    {
        throw new Exception('Second parameter, must be an array of strings');
    }
    foreach ($array_of_tags as $tag)
    {
        // Clean up tags, and trim whitespaces around the tag.
        $needle[]  = "@[\n\r\t ]*(\<[/]?{$tag}\>)[\n\r\t ]*@";
        $replace[]  = '$1';
        // find tags that don't have CDATA, and wrap them with CDATA
        $needle[]  = "@\<{$tag}\>(?!\<\!\[CDATA\[)(.*?)\</{$tag}\>@ms";
        $replace[]  = "<{$tag}><![CDATA[$1]]></{$tag}>";

        $xml_string = preg_replace($needle, $replace, $xml_string);
    }
    return $xml_string;
}

Explanation:

  1. Remove all spaces, tabs, \r, and \n characters around the desired tag, replace with just the tag itself
  2. Find all occurances of $tag, but not followed by <![CDATA[, and wrap contents in <![CDATA[ ... ]]>.
Categories: PHP Tags:

found myself doing the start of a botched code…

December 3rd, 2008 No comments

“XSLT is a pain in the ass”. That was coming out of my mouth… until, you start understanding it. Best tool for the job, XSLT. It’s designed to style XML…

The solution to my problem was easy once you see samples. My problem was XSLT scoping was never discussed in much detail, in any example I saw.

I may eventually write a little code bit, for future reference.

Categories: Uncategorized Tags: