Transform one XML tag into another by PHP and XSL

<?php

// XML/XSL: Transform one tag into another
// You need to have php_xsl.dll in your php extenstions

function transform($xml, $xsl) {
   $xslt = new XSLTProcessor();
   $xslt->importStylesheet(new SimpleXMLElement($xsl));
   return $xslt->transformToXml(new SimpleXMLElement($xml));
}

$sXml  = ‘<xml><sudhir><image id=”model_jpg” name=”model_jpg”>
  <init_from>models/cersi_ships/model.jpg</init_from>
  </image></sudhir></xml>’;

$sXsl  = ‘<xsl:stylesheet
    xmlns:xsl=”http://www.w3.org/1999/XSL/Transform&#8221; version=”1.0″>
    <xsl:output method=”xml”/>

    <xsl:template match=”image/init_from”>
        <init_from2><xsl:value-of select=”.”/></init_from2>
    </xsl:template>

    <xsl:template match=”* | @*”>
        <xsl:copy><xsl:copy-of select=”@*”/><xsl:apply-templates/></xsl:copy>
    </xsl:template>
   
</xsl:stylesheet>’;

echo transform($sXml, $sXsl);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: