
Originally Posted by
Tanus
Brenden: I'm interested to see how you're using XSLT to create the business objects.
This is pretty basic but should get you started. You need php5 for the code below to work. Heres the php:
PHP Code:
// Change this to your class map file's location
$classMap = './examples/class_map.xml';
$xsl = new DOMDocument();
$xsl->load(dirname(__FILE__) .
DIRECTORY_SEPARATOR .
'or_mapping.xsl');
$xsltp = new XSLTProcessor();
$xsltp->importStyleSheet($xsl);
$xml = new DOMDocument();
$xml->load($classMap);
$file = $xsltp->transformToXML($xml);
highlight_string($file);
The class_map.xml file needs to look something like this:
PHP Code:
<orm>
<class name="Team">
<field name="id" />
<field name="name" />
<field name="city" />
</class>
<class name="Player">
<field name="id" />
<field name="name" />
<field name="team" />
</class>
</orm>
You can add more classes, or add more attributes but that is what the basic xml file needs to look like.
The or_mapping.xsl file looks like this:
PHP Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes" />
<xsl:template match="/orm"><?php
<xsl:for-each select="class">
class <xsl:value-of select="@name"/> {
<xsl:for-each select="field">private $<xsl:value-of select="@name" />;
</xsl:for-each>
<xsl:for-each select="field">public function set<xsl:value-of select="translate(substring(@name, 1, 1), 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ')" /><xsl:value-of select="substring(@name, 2, string-length(@name))" />($<xsl:value-of select="@name" />) { $this-><xsl:value-of select="@name" /> = $<xsl:value-of select="@name" />; }
</xsl:for-each>
<xsl:for-each select="field">public function get<xsl:value-of select="translate(substring(@name, 1, 1), 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ')" /><xsl:value-of select="substring(@name, 2, string-length(@name))" />() { return $this-><xsl:value-of select="@name" />; }
</xsl:for-each>
}
</xsl:for-each>
?>
</xsl:template>
</xsl:stylesheet>
I dont know what this does to line endings or whatever but thats what Ive been using. (If anyone cleans that up let me know please
).
The output should look like this:
PHP Code:
class Team {
private $id;
private $name;
private $city;
public function setId($id) { $this->id = $id; }
public function setName($name) { $this->name = $name; }
public function setCity($city) { $this->city = $city; }
public function getId() { return $this->id; }
public function getName() { return $this->name; }
public function getCity() { return $this->city; }
}
class Player {
private $id;
private $name;
private $team;
public function setId($id) { $this->id = $id; }
public function setName($name) { $this->name = $name; }
public function setTeam($team) { $this->team = $team; }
public function getId() { return $this->id; }
public function getName() { return $this->name; }
public function getTeam() { return $this->team; }
}
You can add all kinds of things from here like lazy loading associations, add*() function etc.
Bookmarks