how to compare attribute value using php simplexml?

I need to compare attribute value using php simplexml using below code:
<?php
session_start();
if ( !isset($_SESSION['login']) || $_SESSION['login'] !== true) {
header("location:index.php");
exit;
}

ini_set('display_errors', TRUE);
error_reporting(E_ALL);

$curOpenFile=$_POST['curOpenFile']; // "epub_content/siddhartha3/OPS/package.opf

//echo $curOpenFile;

$data = simplexml_load_file($curOpenFile);
$data->registerXPathNamespace('opf', 'http://www.idpf.org/2007/opf');
$itmref = $data->spine[0]->itemref[0]->attributes();
$itmrefid = $itmref['idref'];

  for($i = 0, $length = count($data->manifest[0]->item); $i < $length; $i++){
     $attr = $data->manifest[0]->item[$i]-> attributes();
     //echo $attr['id'];
    if($attr['id'] == $itmrefid){
    $itmhrefatt = $attr['href'];
	$_SESSION['curbook_first_spine_item'] = $itmhrefatt;
      break;
    }
    
  }

file_put_contents($curOpenFile, $data->saveXML());

//header("location:page1.php");
//exit;
?>

Open in new window


if condition not working. I am unable to find what's wrong in if. Please advice.
JohnLourduAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fabric8Commented:
if ( !isset($_SESSION['login']) || $_SESSION['login'] !== true) {

try changing !== to !=
0
Ray PaseurCommented:
Please post the sample XML document here in the code snippet.  I'll be glad to try to interpret it and show you the code you need.  Thanks, ~Ray
0
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
hi.. this code helps you try this...

$xml = new SimpleXMLElement($xmlString);
$source = $xml->event->attributes()->source;
$timestamp = $xml->event->attributes()->timestamp;
$subject = $xml->event->subject;
$action = $xml->event->action;
$message = $xml->event->message;

where $xmlString is the string of the xml file.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Ray PaseurCommented:
@samirbhogayta: There may be a little more to it than that.  For example, we would need to see the actual content of the XML document in order to identify the tag names.  I favor the process of test-driven development because it will let us test the scripts!
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

And that's why I asked for the XML document, so we would have test data.  Best regards, ~Ray
0
JohnLourduAuthor Commented:
@Ray: I have pasted my xml as below:
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" xml:lang="en" unique-identifier="pub-id">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
    <dc:title id="title">Siddhartha - IGP EPUB3</dc:title>
    <dc:creator id="creator">Herman Hesse</dc:creator>
    <dc:language>en</dc:language>
    <dc:identifier id="pub-id">urn:isbn:siddhartha</dc:identifier>
    <dc:source>urn:isbn:siddhartha</dc:source>
    <meta property="dcterms:modified">2012-01-17T05:02:44Z</meta>
    <dc:date>2010</dc:date>
    <dc:publisher>Infogrid Pacific</dc:publisher>
    <dc:description>THIS VERSION IS LIVE ONLINE FROM THE BLOG. DO NOT ADJUST OR CHANGE. Live demonstration and blog version. Do not modify unless authorized or you really, really know what you are doing.  </dc:description>
    <dc:subject>Novel</dc:subject>
    <dc:type>Book</dc:type>
</metadata>
<manifest>
    <item id="toc" properties="nav" href="TOC.xhtml" media-type="application/xhtml+xml"/>
    <item id="cover" href="cover.xhtml" media-type="application/xhtml+xml"/>
    <item id="cover-image" properties="cover-image" href="images/siddhartha.jpg" media-type="image/jpeg"/>
    <item id="s001" href="s001-BookTitlePage-01.xhtml" media-type="application/xhtml+xml"/>
    <item id="s002" href="s002-Copyright-01.xhtml" media-type="application/xhtml+xml"/>
    <item id="s003" href="s003-Dedication-01.xhtml" media-type="application/xhtml+xml"/>
    <item id="s004" href="s004-Epigraph-01.xhtml" media-type="application/xhtml+xml"/>
    <item id="s005" href="s005-AboutTheAuthor-01.xhtml" media-type="application/xhtml+xml"/>
    <item id="s006" href="s006-AboutThisBook-01.xhtml" media-type="application/xhtml+xml"/>
    <item id="s007" href="s007-Part-001.xhtml" media-type="application/xhtml+xml"/>
    <item id="s008" href="s008-Chapter-001.xhtml" media-type="application/xhtml+xml"/>
    <item id="s009" href="s009-Chapter-002.xhtml" media-type="application/xhtml+xml"/>
    <item id="s010" href="s010-Chapter-003.xhtml" media-type="application/xhtml+xml"/>
    <item id="s011" href="s011-Chapter-004.xhtml" media-type="application/xhtml+xml"/>
    <item id="s012" href="s012-Part-002.xhtml" media-type="application/xhtml+xml"/>
    <item id="s013" href="s013-Chapter-005.xhtml" media-type="application/xhtml+xml"/>
    <item id="s014" href="s014-Chapter-006.xhtml" media-type="application/xhtml+xml"/>
    <item id="s015" href="s015-Chapter-007.xhtml" media-type="application/xhtml+xml"/>
    <item id="s016" href="s016-Chapter-008.xhtml" media-type="application/xhtml+xml"/>
    <item id="s017" href="s017-Chapter-009.xhtml" media-type="application/xhtml+xml"/>
    <item id="s018" href="s018-Chapter-010.xhtml" media-type="application/xhtml+xml"/>
    <item id="s019" href="s019-Chapter-011.xhtml" media-type="application/xhtml+xml"/>
    <item id="s020" href="s020-Chapter-012.xhtml" media-type="application/xhtml+xml"/>
    <item id="s021" href="s021-Appendix-01.xhtml" media-type="application/xhtml+xml"/>
    <item id="images-001" href="images/hermann_hesse_1927_photo_gret_widmann_online.jpg" media-type="image/jpeg"/>
    <item id="css-001" href="css/siddhartha.css" media-type="text/css"/>
</manifest>
<spine page-progression-direction="ltr">
    <itemref idref="s001" linear="yes"/>
    <itemref idref="s002" linear="yes"/>
    <itemref idref="s003" linear="yes"/>
    <itemref idref="s004" linear="yes"/>
    <itemref idref="s005" linear="yes"/>
    <itemref idref="s006" linear="yes"/>
    <itemref idref="s007" linear="yes"/>
    <itemref idref="s008" linear="yes"/>
    <itemref idref="s009" linear="yes"/>
    <itemref idref="s010" linear="yes"/>
    <itemref idref="s011" linear="yes"/>
    <itemref idref="s012" linear="yes"/>
    <itemref idref="s013" linear="yes"/>
    <itemref idref="s014" linear="yes"/>
    <itemref idref="s015" linear="yes"/>
    <itemref idref="s016" linear="yes"/>
    <itemref idref="s017" linear="yes"/>
    <itemref idref="s018" linear="yes"/>
    <itemref idref="s019" linear="yes"/>
    <itemref idref="s020" linear="yes"/>
    <itemref idref="s021" linear="yes"/>
</spine>
</package>

Open in new window

0
Ray PaseurCommented:
There are "legit" ways to process a namespace, but in practice I have found this quick-and-dirty process to be quite useful.  See line 80 and the function on line 115.  This lets us access the namespaced elements as shown in lines 89-92.

The foreach() iterator is quite useful with PHP objects that contain other objects or arrays, and as always, var_dump() is your friend.

<?php // RAY_temp_johnlourdu.php
error_reporting(E_ALL);
echo '<pre>';

// PROCESS SOME XML
// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28243737.html

// TEST DATA FROM THE POST AT EE
$xml = <<<EOD
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" xml:lang="en" unique-identifier="pub-id">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
    <dc:title id="title">Siddhartha - IGP EPUB3</dc:title>
    <dc:creator id="creator">Herman Hesse</dc:creator>
    <dc:language>en</dc:language>
    <dc:identifier id="pub-id">urn:isbn:siddhartha</dc:identifier>
    <dc:source>urn:isbn:siddhartha</dc:source>
    <meta property="dcterms:modified">2012-01-17T05:02:44Z</meta>
    <dc:date>2010</dc:date>
    <dc:publisher>Infogrid Pacific</dc:publisher>
    <dc:description>THIS VERSION IS LIVE ONLINE FROM THE BLOG. DO NOT ADJUST OR CHANGE. Live demonstration and blog version. Do not modify unless authorized or you really, really know what you are doing.  </dc:description>
    <dc:subject>Novel</dc:subject>
    <dc:type>Book</dc:type>
</metadata>
<manifest>
    <item id="toc" properties="nav" href="TOC.xhtml" media-type="application/xhtml+xml"/>
    <item id="cover" href="cover.xhtml" media-type="application/xhtml+xml"/>
    <item id="cover-image" properties="cover-image" href="images/siddhartha.jpg" media-type="image/jpeg"/>
    <item id="s001" href="s001-BookTitlePage-01.xhtml" media-type="application/xhtml+xml"/>
    <item id="s002" href="s002-Copyright-01.xhtml" media-type="application/xhtml+xml"/>
    <item id="s003" href="s003-Dedication-01.xhtml" media-type="application/xhtml+xml"/>
    <item id="s004" href="s004-Epigraph-01.xhtml" media-type="application/xhtml+xml"/>
    <item id="s005" href="s005-AboutTheAuthor-01.xhtml" media-type="application/xhtml+xml"/>
    <item id="s006" href="s006-AboutThisBook-01.xhtml" media-type="application/xhtml+xml"/>
    <item id="s007" href="s007-Part-001.xhtml" media-type="application/xhtml+xml"/>
    <item id="s008" href="s008-Chapter-001.xhtml" media-type="application/xhtml+xml"/>
    <item id="s009" href="s009-Chapter-002.xhtml" media-type="application/xhtml+xml"/>
    <item id="s010" href="s010-Chapter-003.xhtml" media-type="application/xhtml+xml"/>
    <item id="s011" href="s011-Chapter-004.xhtml" media-type="application/xhtml+xml"/>
    <item id="s012" href="s012-Part-002.xhtml" media-type="application/xhtml+xml"/>
    <item id="s013" href="s013-Chapter-005.xhtml" media-type="application/xhtml+xml"/>
    <item id="s014" href="s014-Chapter-006.xhtml" media-type="application/xhtml+xml"/>
    <item id="s015" href="s015-Chapter-007.xhtml" media-type="application/xhtml+xml"/>
    <item id="s016" href="s016-Chapter-008.xhtml" media-type="application/xhtml+xml"/>
    <item id="s017" href="s017-Chapter-009.xhtml" media-type="application/xhtml+xml"/>
    <item id="s018" href="s018-Chapter-010.xhtml" media-type="application/xhtml+xml"/>
    <item id="s019" href="s019-Chapter-011.xhtml" media-type="application/xhtml+xml"/>
    <item id="s020" href="s020-Chapter-012.xhtml" media-type="application/xhtml+xml"/>
    <item id="s021" href="s021-Appendix-01.xhtml" media-type="application/xhtml+xml"/>
    <item id="images-001" href="images/hermann_hesse_1927_photo_gret_widmann_online.jpg" media-type="image/jpeg"/>
    <item id="css-001" href="css/siddhartha.css" media-type="text/css"/>
</manifest>
<spine page-progression-direction="ltr">
    <itemref idref="s001" linear="yes"/>
    <itemref idref="s002" linear="yes"/>
    <itemref idref="s003" linear="yes"/>
    <itemref idref="s004" linear="yes"/>
    <itemref idref="s005" linear="yes"/>
    <itemref idref="s006" linear="yes"/>
    <itemref idref="s007" linear="yes"/>
    <itemref idref="s008" linear="yes"/>
    <itemref idref="s009" linear="yes"/>
    <itemref idref="s010" linear="yes"/>
    <itemref idref="s011" linear="yes"/>
    <itemref idref="s012" linear="yes"/>
    <itemref idref="s013" linear="yes"/>
    <itemref idref="s014" linear="yes"/>
    <itemref idref="s015" linear="yes"/>
    <itemref idref="s016" linear="yes"/>
    <itemref idref="s017" linear="yes"/>
    <itemref idref="s018" linear="yes"/>
    <itemref idref="s019" linear="yes"/>
    <itemref idref="s020" linear="yes"/>
    <itemref idref="s021" linear="yes"/>
</spine>
</package>
EOD;

// MAKE THE NAMESPACE DATA ACCESSIBLE
$xml = mungXML($xml);

// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// SHOW SOME OF THE OBJECT PROPERTIES, CAST AS STRINGS
$v = (string)$obj->attributes()->version;
echo PHP_EOL . "THE VERSION IS $v.";

$t = (string)$obj->metadata->dc_title;
$c = (string)$obj->metadata->dc_creator;
$p = (string)$obj->metadata->dc_publisher;
echo PHP_EOL . "TITLE: $t. BY: $c. PUBLISHED BY: $p.";

// EXTRACT SPINE->ITEMREF ATTRIBUTES
$arr = array();
foreach ($obj->spine->itemref as $itemref)
{
    $r = (string)$itemref->attributes()->idref;
    $arr[$r] = (string)$itemref->attributes()->linear;
}

// USE AN ITERATOR TO LIST DATA FROM THE OBJECT
foreach ($obj->manifest->item as $item)
{
    $i = (string)$item->attributes()->id;
    $h = (string)$item->attributes()->href;
    echo PHP_EOL . '<a href="' . $h . '">' . $i . '</a> ';

    // IF THERE IS A SPINE->ITEMREF MATCH
    if (array_key_exists($i, $arr)) echo $arr[$i];
}


// A FUNCTION TO MUNG THE XML SO WE DO NOT HAVE TO DEAL WITH NAMESPACE
function mungXML($xml)
{
    $obj = SimpleXML_Load_String($xml);
    if (!$obj) return $xml;

    // GET NAMESPACES, IF ANY
    $nss = $obj->getNamespaces(TRUE);
    if (empty($nss)) return $xml;

    // CHANGE ns: INTO ns_
    $nsm = array_keys($nss);
    foreach ($nsm as $key)
    {
        // A REGULAR EXPRESSION TO MUNG THE XML
        $rgx
        = '#'               // REGEX DELIMITER
        . '('               // GROUP PATTERN 1
        . '\<'              // LOCATE A LEFT WICKET
        . '/?'              // MAYBE FOLLOWED BY A SLASH
        . preg_quote($key)  // THE NAMESPACE
        . ')'               // END GROUP PATTERN
        . '('               // GROUP PATTERN 2
        . ':{1}'            // A COLON (EXACTLY ONE)
        . ')'               // END GROUP PATTERN
        . '#'               // REGEX DELIMITER
        ;
        // INSERT THE UNDERSCORE INTO THE TAG NAME
        $rep
        = '$1'          // BACKREFERENCE TO GROUP 1
        . '_'           // LITERAL UNDERSCORE IN PLACE OF GROUP 2
        ;
        // PERFORM THE REPLACEMENT
        $xml =  preg_replace($rgx, $rep, $xml);
    }
    return $xml;
}

Open in new window

HTH, ~Ray
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.