What is the problems for my xml file format ?

Posted by python on Stack Overflow See other posts from Stack Overflow or by python
Published on 2010-12-22T10:28:01Z Indexed on 2010/12/22 10:54 UTC
Read the original article Hit count: 183

Filed under:
|
|
<?
$xml="<?xml version='1.0' encoding='UTF-8' standalone='no'?>
  <Document>
      <pain.001.001.02>
    <books>    
        <book>
        <qty>12</qty>
        <title>C++</title>
        </book>
        <book>
        <qty>21</qty>
        <title>PHP</title>
        </book>    
      </books>    

      <books>    
        <book>
        <qty>25</qty>
        <title>Java</title>
        </book>    
        <book>  
        <qty>32</qty>
        <title>Python</title>
        </book>
        <book>  
        <qty>22</qty>
        <title>History</title>
        </book>    
    </books>
  </pain.001.001.02>
  </Document> ";

$doc = new DOMDOcument;
$doc->loadxml($xml);

$xpath = new DOMXpath($doc);

$arr = array(
array(
      '12;C++',
      '21;PHP'),
array(
      '25;Java',
      '32;Python'
)
  );

# Remove elements based on qty and title
foreach($arr as $items) {
foreach($items as $item) {
    list($qty, $title) = explode(';', $item);
    foreach($xpath->query('//pain.001.001.02/books/book[title="'.$title.'"][qty="'.$qty.'"]') as $book) {
    $book->parentNode->removeChild($book);
    }
}
}

# Remove empty <books>
foreach($xpath->query('pain.001.001.02/books[count(book)=0]') as $empty) {
$empty->parentNode->removeChild($empty);
}

header('Content-type: text/xml');
echo $doc->savexml();


 ?> 

If I put <Document> in the xml document , get the expected result BUT If change <Document> to

<Document
     xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.02">

get UNEXPECTED RESULT?

Do you have any idea?

thanks

© Stack Overflow or respective owner

Related posts about php

Related posts about Xml