Forum Moderators: open

Message Too Old, No Replies

converting to php5 - xpath troubles

converting to php5 - xpath troubles

         

silkatron

2:48 pm on May 9, 2006 (gmt 0)

10+ Year Member



After a 95% successful convert to php5 dom from php4 domxml I find myself with a rather irritating xpath change which i've been banging my head against the wall with. Let me show you the original php4 code:

if ( $dom = domxml_open_file( $root."xml/sidebar.xml" ) )
{
$context = xpath_new_context( $dom );

$blocks = xpath_eval( $context, "/sidebar/block[@id=/sidebar/group[@id='$sidebar']/block-ref/@id]" );

$blocks = $blocks->nodeset;

$xml .= "<sidebar>";

while( $block = each( $blocks ) )
{
$block = $block[1];

$context = xpath_new_context( $block );

$xml .= $dom->dump_node( $block );
}

$xml .= "</sidebar>";
}

I'm basically getting a little lost with the xpath reading the blocks tree. Below is a sample of the xml it is reading:

<sidebar>
<block id="catno">
<name>Find HTFR cat number</name>
<catno/>
</block>

<block id="logon">
<logon/> <!-- xsl stuff -->
</block>

<block id="newsite">
<name>htfr site enhancements</name>
<p>Let us know what you think of our new site, or of any bugs you may have found.</p>
<link>Click here to email us</link>
</block>
</sidebar>

Please can someone help me solve this problem or at least enlighten me about the change in xpath.

choster

4:33 pm on May 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't see any <block-ref>s.

silkatron

9:02 am on May 11, 2006 (gmt 0)

10+ Year Member



I managed to work this out in the end
Here is the code I used to achieve the same results:

$dom->load( $root.'xml/sidebar.xml' );

$xpath = new DomXPath($dom);

$blocks = $xpath->query( "/sidebar/block[@id=/sidebar/group[@id='$sidebar']/block-ref/@id]" );

$xml .= "<sidebar>";

foreach( $blocks as $block )
{
$xml .= $dom->saveXML( $block );
}

$xml .= "</sidebar>";

Rather simpler code than php4 but was a bugger to work out!