2011-05-31 52 views
0

我的XML是這樣的:檢查是否<p1>標籤不存在<M>添加此<p1></p1>進去

<?xml version="1.0"?> 
<document> 
    <consonant> 
    <L> 
     <l1>l1</l1> 
    </L> 
    <M> 
     <m1>m1</m1> 
    </M> 
    <N> 
     <n1>n1</n1> 
    </N> 
    </consonant> 

    <consonant> 
     <L> 
     <l1>l1</l1> 
     </L> 
     <M> 
     <m1>m1</m1> 
     </M> 
     <N> 
     <n1>n1</n1> 
     </N> 
     </consonant> 

</document> 

檢查,如果在add this <p1><a>1223</a></p1>進去<p1> tag is not exist

<consonant> 
     <L> 
     <l1>l1</l1> 
     </L> 
     <M> 
     <m1>m1</m1> 
     <p1><a>1223</a></p1> 
     </M> 
     <N> 
     <n1>n1</n1> 
     </N> 
    </consonant> 

我想:

$xml = simplexml_load_string($myxml); // return object 
    $nss = $xml->getDocNamespaces(TRUE); 
    $xml->registerXPathNamespace('__empty_ns', $nss[""]); 
    $result = $xml->xpath("/__empty_ns:Document/__empty_ns:consonant");//consonant node 
    foreach($result as $key=>$value){ // loop through all <consonant> 
      if(!array_key_exist('p1')){ // if not exist 
       $value['p1'] = // add node here <p1><a>1223</a></p1> 
      } 
    } 

,並使用它的對象操作(condifion,循環,追加,.)

任何人都可以告訴我,我該怎麼辦呢?

+0

所以你試着用SimpleXML加載XML,這就是它?你還試過了什麼?顯示你的腳本。它工作嗎?如果是這樣,爲什麼你不需要另一種解決方案?另外,你可以澄清你正在嘗試做什麼,因爲'檢查標記是否不存在添加這個成它。'對我沒有多大意義 – Gordon 2011-05-31 08:23:21

+0

聽起來像對我來說是正確的方法。但是我們看不到你在做什麼,而你沒有提供「更好」的定義。也許更深入的問題會更適合codereview.stackexchange.com? – 2011-05-31 08:26:05

+0

是的,謝謝這兩個意見,我解開了這個問題。感謝 – sophie 2011-05-31 08:35:11

回答

0

你可以嘗試這樣的事:

<?php 
$dom = new DOMDocument(); 
$dom->loadXML('<?xml version="1.0"?> 
<document 
    xmlns:xsd="w3.org/2001/XMLSchema" 
    xmlns:xsi="w3.org/2001/XMLSchema-instance" 
    xmlns="urn:iso:std:iso:20022:tech:xsd:test.001.001.02" 
> 
    <consonant> 
     <L> 
      <l1>l1</l1> 
     </L> 
     <M> 
      <m1>m1</m1> 
     </M> 
     <N> 
      <n1>n1</n1> 
     </N> 
    </consonant> 

    <consonant> 
     <L> 
      <l1>l1</l1> 
     </L> 
     <M> 
      <m1>m1</m1> 
      <p1>unchanged</p1> 
     </M> 
     <N> 
      <n1>n1</n1> 
     </N> 
    </consonant> 
</document>'); 

$xpath = new DOMXPath($dom); 
$xpath->registerNameSpace('what-ever', 'urn:iso:std:iso:20022:tech:xsd:test.001.001.02'); // 'what-ever' can be anything other than an empty string 

foreach ($xpath->query('//what-ever:consonant/what-ever:M[not(what-ever:p1)]') as $node) { // select M-nodes without p1-nodes as children 
    $p1Node = $node->appendChild($dom->createElement('p1')); 

    // do stuff with $p1Node 
    $aNode = $p1Node->appendChild($dom->createElement('a', '1234')); 
} 

header('Content-Type: text/xml; charset="utf-8"'); 
echo $dom->saveXML(); 

輸出

<?xml version="1.0"?> 
<document> 
    <consonant> 
     <L> 
      <l1>l1</l1> 
     </L> 
     <M> 
      <m1>m1</m1> 
      <p1><a>1234</a></p1></M> 
     <N> 
      <n1>n1</n1> 
     </N> 
    </consonant> 

    <consonant> 
     <L> 
      <l1>l1</l1> 
     </L> 
     <M> 
      <m1>m1</m1> 
      <p1>unchanged</p1> 
     </M> 
     <N> 
      <n1>n1</n1> 
     </N> 
    </consonant> 
</document> 
+0

是的,它適用於。但是如果我改爲它不起作用。有什麼想法? – sophie 2011-05-31 09:11:02

+0

@Transformer查看更新後的示例。 :) – Yoshi 2011-05-31 09:24:47