E wrote:
Quote:
I'm new to XML and I'm just wondering if someone could
point me to some tutorials on how to generate an XML file
using PHP?
>
What I'm trying to do is to use data from the MySQL table
below to generate an XML file which I will then use to
display that info as a hierarchy.
|
[relational data]
Quote:
First of all, how do I arrange the above data into an XML
file
|
It's very straightforward:
<?php
error_reporting ( E_ALL ) ;
$data =
array
(
array
(
'id' =1 ,
'parent_id' =NULL ,
'name' ='about us' ,
) ,
/* *************************
* the rest of your data *
************************* */
) ;
function genxml ( $data , $parent = NULL )
{
$result = '' ;
foreach ( $data as $element )
{
if ( $element [ 'parent_id' ] == $parent )
{
$element [ 'name' ] =
preg_replace
(
'/\s+/' ,
'_' ,
$element [ 'name' ]
) ;
$element [ 'no' ] =
preg_replace
(
'/^.*?(\d*)$/' ,
' no="\1"' ,
$element [ 'name' ]
) ;
$element [ 'name' ] =
preg_replace
(
'/[\d\.]+$/' ,
'' ,
$element [ 'name' ]
) ;
$result .=
'<' . $element [ 'name' ] .
(
$element [ 'no' ] != ' no=""'
? $element [ 'no' ]
: ''
) .
'>' ;
$result .= genxml ( $data , $element [ 'id' ] ) ;
$result .= '</' . $element [ 'name' ] . '>' ;
}
}
return $result ;
}
header ( 'Content-Type: text/xml' ) ;
print
(
'<?xml-stylesheet type="text/xsl" href="genxml.xsl"?>'
) ;
print ( '<data>' ) ;
print ( genxml ( $data ) ) ;
print ( '</data>' ) ;
?>
Quote:
and then, how do I read the data from the file and
display it like the list above?
|
The following transformation (referred above as
'genxml.xsl') does that:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/data">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="*">
<xsl:param name="indent" select="''"/>
<xsl:value-of select="$indent"/>
<xsl:text>- </xsl:text>
<xsl:apply-templates select="." mode="get-name"/>
<xsl:if test="@no">
<xsl:call-template name="get-no"/>
</xsl:if>
<xsl:text>
</xsl:text>
<xsl:apply-templates>
<xsl:with-param
name="indent"
select="concat($indent,' ')"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="about_us" mode="get-name">
<xsl:text>About Us</xsl:text>
</xsl:template>
<xsl:template match="products" mode="get-name">
<xsl:text>Products</xsl:text>
</xsl:template>
<xsl:template match="contact_us" mode="get-name">
<xsl:text>Contact Us</xsl:text>
</xsl:template>
<xsl:template match="product" mode="get-name">
<xsl:text>Product</xsl:text>
</xsl:template>
<xsl:template match="category" mode="get-name">
<xsl:text>Category</xsl:text>
</xsl:template>
<xsl:template match="subcategory" mode="get-name">
<xsl:text>SubCategory</xsl:text>
</xsl:template>
<xsl:template name="get-no">
<xsl:text</xsl:text>
<xsl:apply-templates select=".." mode="get-no"/>
<xsl:value-of select="@no"/>
</xsl:template>
<xsl:template match="*[not(@no)]" mode="get-no"/>
<xsl:template match="*[@no]" mode="get-no">
<xsl:apply-templates select=".." mode="get-no"/>
<xsl:value-of select="@no"/>
<xsl:text>.</xsl:text>
</xsl:template>
</xsl:stylesheet>
Figuring out how does all of that work is left as a
(potentially enlightening) exercise for the reader.
--
Pavel Lepin