Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP & XML - Load node via dynamic url variables

Status
Not open for further replies.

MrT2005

Programmer
Mar 8, 2005
22
CA
I have 3 pages

Page 1 is php to process & display xml (via sablotron)
Page 2 is xml - contains the data
Page 3 is the xsl file for style

How do I load one record from the XML file
via a dynamic url variable ?

ie. geo_area.php?record=33

the code I have for loading the xml + xsl in php
is this:

<?php
function LoadPageContent($wFile,$wStyle){
# create XSLT processor
$xsltProcessor = xslt_create ();

# generate filebase from working directory
$fileBase = 'file://' . getcwd () . '/';

# process the 'sample.xml' and 'sample.xsl' files
$result = xslt_process ($xsltProcessor, $fileBase . "xml/" . $wFile .'.xml', $fileBase . "xml/" . $wStyle . '.xsl');

# print the result
if ( $result ) {
echo $result; //htmlentities ( $result );
} else {
echo 'Fail due to error: '.xslt_error($xsltProcessor);
}
# free the processor resource
xslt_free ( $xsltProcessor );
}
?>

anyone have samples or good demos ?

thanks!
 
records.xml :

<?xml version="1.0" encoding="UTF-8"?>

<?xml-stylesheet href="regionChoix.xsl" type="text/xsl"?>
<regionX>

<region1 id="1" column="1">
<sURL>page-e.php</sURL>
<sText>Attraits patrimoniaux</sText>
<sDesc>Lorem ipsum dolor</sDesc> <imageBox><sIMG>rsrc/img/img_categorie1.gif</sIMG></imageBox>
</region1>

<region1 id="2" column="1">
<sURL>page-e.php</sURL>
<sText>Attraits patrimoniaux</sText>
<sDesc>Lorem ipsum dolor</sDesc> <imageBox><sIMG>rsrc/img/img_categorie1.gif</sIMG></imageBox>
</region1>

</regionX>


regionstyle.xsl :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="
<xsl:eek:utput method="html"/>

<xsl:template match="regionX">
<br />
<xsl:for-each select="region1">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="*" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="110" valign="top">

<IMG>
<!-- -->
<xsl:attribute name="ALT">
<xsl:text></xsl:text>
</xsl:attribute>
<!-- -->
<xsl:attribute name="BORDER">
<xsl:text>0</xsl:text>
</xsl:attribute>
<!-- -->
<xsl:attribute name="HEIGHT">
<xsl:text>107</xsl:text>
</xsl:attribute>
<!-- -->
<xsl:attribute name="SRC">
<xsl:value-of select="imageBox/sIMG"/>
</xsl:attribute>
<!-- -->
<xsl:attribute name="WIDTH">
<xsl:text>110</xsl:text>
</xsl:attribute>
<!-- -->
</IMG>

</td>
<td width="*" valign="top" class="pad">
<h3>
<xsl:value-of select="sText" />
</h3>
<xsl:value-of select="sDesc" />
</td>
</tr>
</table>
</td>
<td width="110" valign="top">
<img src="rsrc/img/E_listeRegCat.gif" />
</td>
</tr>
</table>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>
 
You could DOM methods to alter the XSL file:

Code:
foreach ($xsl->getElementsByTagName('xsl:for-each') as $element) {
  $element->setAttribute("select", "region1[@id = " + myid + "]");
}
Or use XPath to get a nodeset:
Code:
$dom = DomDocument::loadXML($xml);
$xpath = new domxpath($dom);

$record = $xpath->query("regionX/region1[@id = " + myid + "]");

For more info, see:


ps:
Code:
<IMG>
  <!-- -->
  <xsl:attribute name="ALT">
  <xsl:text></xsl:text>
  </xsl:attribute>
  <!-- -->
  <xsl:attribute name="BORDER">
  <xsl:text>0</xsl:text>
  </xsl:attribute>
  <!-- -->
  <xsl:attribute name="HEIGHT">
  <xsl:text>107</xsl:text>
  </xsl:attribute>
  <!-- -->
  <xsl:attribute name="SRC">
  <xsl:value-of select="imageBox/sIMG"/>  
  </xsl:attribute>
  <!-- -->
  <xsl:attribute name="WIDTH">
  <xsl:text>110</xsl:text>
  </xsl:attribute>
  <!-- -->
</IMG>
Could be shorten to:
Code:
<img alt=""
border="0" height="107" src="{imageBox/sIMG}" width="110" />
 
Im running PHP Version 4.3.3 which uses domxml as opposed to PHP 5 which uses dom.

your code causes "Fatal error: Cannot instantiate non-existent class: DomDocument"

any ideas?

thanks!
 
I dunno really, I've never used PHP. Just loooking around that site. Try this:
Code:
$dom = domxml_open_file('sample.xml')

$xpath = xpath_new_context($dom);
nodeObject = xpath_eval_expression($xpath, "regionX/region1[@id = " + myid + "]");
 
still getting errors, I'm going to udgrade to php5 and try the code on there.

thanks
 
ok, PHP5 is more flexible than PHP4 with sablotron.

can create functions,classes to my hearts content!

sweet!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top