2 回答

TA貢獻1864條經驗 獲得超2個贊
這是一個 XML 命名空間前綴,如果沒有匹配的 XML 命名空間定義,它是無效的。DOM 有特殊的方法來創建帶有命名空間的 XML 節點:
// a list of namespaces just to avoid repeating the URIs
// the keys do not have to match the prefixes in the XML
$namespaces = [
'xmlns' => 'http://www.w3.org/2000/xmlns/',
'a' => 'urn:example:a',
'i' => 'urn:example:i'
];
$document = new DOMDocument();
// the document element does not seem to have a namespace
$identifiant = $document->appendChild(
$document->createElement('identifiant')
);
// add the namespace definition for prefix "a"
// namespace definition use a reserved, internal namespace
$identifiant->setAttributeNS($namespaces['xmlns'], 'xmlns:a', $namespaces['a']);
$identifiant->appendChild(
// create an elment node with the namespace
$document->createElementNS(
$namespaces['a'],
'a:Nom'
)
)->textContent = 'NOM';
$identifiant
->appendChild(
$document->createElementNS(
$namespaces['a'],
'a:NomJeuneFille'
)
)
->setAttributeNS(
$namespaces['i'],
'i:nil',
'true'
);
$identifiant->appendChild(
$document->createElementNS(
$namespaces['a'],
'a:Prenom'
)
)->textContent = 'PRENOM';
$document->formatOutput = TRUE;
echo $document->saveXML();
輸出:
<?xml version="1.0"?>
<identifiant xmlns:a="urn:example:a">
<a:Nom>NOM</a:Nom>
<a:NomJeuneFille xmlns:i="urn:example:i" i:nil="true"/>
<a:Prenom>PRENOM</a:Prenom>
</identifiant>
將根據需要添加命名空間定義。您可以通過在祖先節點上手動定義它來避免在兄弟分支上重復它。我對“a”前綴執行此操作,“i”前綴的定義是自動添加的。

TA貢獻1868條經驗 獲得超4個贊
嘗試對所選內容進行查找和替換(大多數文本編輯器允許您執行此操作)
如果您的文件看起來相似,您可以通過濫用以空格開頭的想法來以最小的努力替換開始塊
尋找
<
代替
<a:
如果這還不夠(即您有一個更復雜的文件或希望對大量文件執行此操作),您將需要使用 XML 解析器重排您的 XML 文檔
一個程序看起來像
import XML parsing library
read XML file
iterate over the blocks of whatever depth until you find what you want
change the block names (perhaps copy to new blocks and switch 'em)
write the resulting file again (consider writing to a new file so you can compare them more easily)
- 2 回答
- 0 關注
- 194 瀏覽
添加回答
舉報