簡體   English   中英

將帶有冒號的屬性添加到php中的xml節點

[英]add attribute with colon to xml node in php

我在php變量中有一個xml值,我需要添加屬性xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd" and xmlns="http://www.loc.gov/MARC21/slim" “ record”元素上的xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd" and xmlns="http://www.loc.gov/MARC21/slim" 下面是我在php中的xml

 <record xmlns="http://www.loc.gov/MARC21/slim"> <leader>01344cam a2200385 i 4500</leader> <controlfield tag="001">19196211</controlfield> <controlfield tag="005">20160810165833.0</controlfield> <controlfield tag="008">160728t20162015nyu 000 1 eng </controlfield> <datafield tag="906" ind1=" " ind2=" "> <subfield code="a">7</subfield> <subfield code="b">cbc</subfield> <subfield code="c">orignew</subfield> <subfield code="d">2</subfield> <subfield code="e">ncip</subfield> <subfield code="f">20</subfield> <subfield code="g">y-gencatlg</subfield> </datafield> </record> 

其他字符正在代替冒號,但冒號沒有起作用。 我知道它有一些與名稱空間有關的東西,但是我無法弄清楚這個問題。 任何幫助表示贊賞。

當添加帶有:的屬性時,正如您所說的添加帶有名稱空間的屬性一樣。 您需要做的是確保已定義此名稱空間。 因此,在添加屬性時,將第三個參數用作名稱空間的URI ...

$nodes = $xml->xpath('//zs:record//def:record')[0];
$nodes[0]->addAttribute('xsi:schemaLocation', 'http://www.loc.gov/MARC21/slim',
        'http://www.w3.org/2001/XMLSchema-instance');

echo $nodes[0]->asXML();

給...

<record xmlns="http://www.loc.gov/MARC21/slim" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.loc.gov/MARC21/slim">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM