$file1 = "http://<URL>/properties2.xml";
$newxml="beauxvillages.xml";
if (file_exists($origdir.$newxml)) {
unlink($origdir.$newxml);
}
$file=fopen($origdir.$newxml,"a");
$_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";
$_xml .="<Properties>\r\n";
$i=0;
$xmlReader = new XMLReader();
$xmlReader->open($file1);
while($xmlReader->read()) {
// check to ensure nodeType is an Element not attribute or #Text
if($xmlReader->nodeType == XMLReader::ELEMENT) {
if($xmlReader->localName == 'property') {
$_xml .="\t<Property>\r\n";
$_xml .="\t\t<Agent>BV</Agent>\r\n";
$_xml .="\t\t<AgentName>Beaux Villages</AgentName>\r\n";
$id = $xmlReader->getAttribute('reference');
$_xml .="\t\t<ID>".$id."</ID>\r\n";
}
if($xmlReader->localName == 'advert_heading') {
$xmlReader->read();
$title = $xmlReader->value;
$_xml .="\t\t<Title>".$title."</Title>\r\n";
}
if($xmlReader->localName == 'main_advert') {
$xmlReader->read();
$description = $xmlReader->value;
$_xml .="\t\t<Description>".$description."</Description>\r\n";
}
if($xmlReader->localName == 'town') {
$xmlReader->read();
$town = $xmlReader->value;
$_xml .="\t\t<City>".$town."</City>\r\n";
}
if($xmlReader->localName == 'postcode') {
$xmlReader->read();
$postcode = $xmlReader->value;
$postcode = str_replace(",France","",$postcode);
$_xml .="\t\t<Postcode>".$postcode."</Postcode>\r\n";
}
if($xmlReader->localName == 'property_type') {
$xmlReader->read();
$type = $xmlReader->value;
$_xml .="\t\t<PropertyType>".$type."</PropertyType>\r\n";
}
if($xmlReader->localName == 'numeric_price') {
$xmlReader->read();
$price = $xmlReader->value;
$_xml .="\t\t<Price>".(int)$price."</Price>\r\n";
}
if($xmlReader->localName == 'bedrooms') {
$xmlReader->read();
$bedrooms = $xmlReader->value;
$_xml .="\t\t<Bedrooms>".$bedrooms."</Bedrooms>\r\n";
}
if($xmlReader->localName == 'bathrooms') {
$xmlReader->read();
$bathrooms = $xmlReader->value;
$_xml .="\t\t<Bathrooms>".$bathrooms."</Bathrooms>\r\n";
}
if($xmlReader->localName == 'number1') {
$xmlReader->read();
$plot = $xmlReader->getAttribute('value');
$_xml .="\t\t<PlotSize>".$plot."</PlotSize>\r\n";
}
if($xmlReader->localName == 'floorplans') {
// got to end
$_xml .="\t\t<WCs></WCs>\r\n";
$_xml .="\t\t<Locality></Locality>\r\n";
$_xml .="\t\t<Status>For Sale</Status>\r\n";
$_xml .="\t</Property>\r\n";
$i++;
}
}
}
$_xml .="</Properties>\r\n";
fwrite($file, $_xml);
fclose($file);
if($xmlReader->localName == 'property') {
$id = $xmlReader->getAttribute('reference');
$_xml .="\t\t<ID>".$id."</ID>\r\n";
}
But this doesn't and I can't see whyif($xmlReader->localName == 'number1') {
$xmlReader->read();
$plot = $xmlReader->getAttribute('value');
$_xml .="\t\t<PlotSize>".$plot."</PlotSize>\r\n";
}
//check if the "current/active" node's name equals 'number1'
if($xmlReader->localName == 'number1') {
//if so, make the next node the "current/active" node
$xmlReader->read();
//now get the 'value' attribute from the "current/active" node.
$plot = $xmlReader->getAttribute('value');
//append <PlotSize> onto variable.
$_xml .="\t\t<PlotSize>".$plot."</PlotSize>\r\n";
}
//check if the "current/active" node's name equals 'number1'
if($xmlReader->localName == 'number1') {
//if so, make the next node the "current/active" node
$xmlReader->read();
if( substr($xmlReader->localName,0,6)=='number')
{
//now get the 'value' attribute from the "current/active" node.
$plot = $xmlReader->getAttribute('value');
}
else
{
//this should tell you which node it is currently reading
echo 'no value attribute detected on node '. $xmlReader->localName;
$plot=0;
}
//append <PlotSize> onto variable.
$_xml .="\t\t<PlotSize>".$plot."</PlotSize>\r\n";
}
<pictures>
<picture name="Photo 1">
<filename>http://www.etc.JPG</filename>
</picture>
<picture name="Photo 2">
<filename>http://www.etc.JPG</filename>
</picture>
<picture name="Photo 3">
<filename>http://www.etc.JPG</filename>
</picture>
etc ....
</pictures>
I need to convert that to the following format<imageCount>8</imageCount>
<Images>
<Image1>http://www.etc.jpg</Image1>
<Image2>http://www.etc.jpg</Image2>
<Image3>http://www.etc.jpg</Image3>
etc...
</Images>
The total number of images varies. There doesn't appear to be a maximum number of images and the Name attributes don't follow a pattern.<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/agency/branches/branch">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="properties">
<!-- This "if" clause is meant to exclude branches with no properties ex: Sales Support -->
<xsl:if test="count(*) > 0">
<xsl:element name="Properties">
<xsl:attribute name="branch"><xsl:value-of select="../@name"/></xsl:attribute>
<xsl:apply-templates select="property"/>
</xsl:element>
</xsl:if>
</xsl:template>
<xsl:template match="property">
<Property>
<Agent>BV</Agent>
<AgentName>Beaux Villages</AgentName>
<ID><xsl:value-of select="@reference"/></ID>
<Title><xsl:value-of select="advert_heading"/></Title>
<Description><xsl:value-of select="main_advert"/></Description>
<City><xsl:value-of select="town"/></City>
<PostCode><xsl:value-of select="translate(postcode,'France','')"/></PostCode>
<PropertyType><xsl:value-of select="property_type"/></PropertyType>
<Price><xsl:value-of select="numeric_price"/></Price>
<Bedrooms><xsl:value-of select="bedrooms"/></Bedrooms>
<Bathrooms><xsl:value-of select="bathrooms"/></Bathrooms>
<PlotSize><xsl:value-of select="number1/@value"/></PlotSize>
<WCs></WCs>
<Locality></Locality>
<Status>For Sale</Status>
<Pictures><xsl:apply-templates select="pictures/picture"/></Pictures>
</Property>
</xsl:template>
<xsl:template match="picture">
<Picture>
<!-- These will add and "id" and "caption" attribute to the enclosing element "Picture" in the output -->
<xsl:attribute name="id"><xsl:value-of select="position()"/></xsl:attribute>
<xsl:attribute name="caption"><xsl:value-of select="@name"/></xsl:attribute>
<!-- This retrieves the text-node value from filename and uses it as the text-node value for "Picture" (because it is enclosed in the "Picture" node) -->
<xsl:value-of select="filename" />
</Picture>
</xsl:template>
</xsl:stylesheet>
<?php
//hielo.php
// Load the XML source
$xml = new DOMDocument;
# you need to provide the correct path below so that it "points" to the exact location
# of properties2.xml
$xml->load('/path/to/properties2.xml');
$xsl = new DOMDocument;
# this too needs the exact location of the xsl file
$xsl->load('/path/to/hielo.xsl');
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules
# here provide the correct path of where you want the output saved.
file_put_contents('/path/to/outputFile.xml', $proc->transformToXml($xml) );
?>
http://uk3.php.net/manual/en/book.xmlreader.php
If nobody else comes up with an example I will get one working for you tomorrow.