我有以下XML,我需要用数组中的一些数据填充它。 问题是它有3个节点,名称为“contact”,但每个节点都有不同的属性。 我尝试使用Xpath,但我没有设法让它工作不断得到错误。 我认为这可能是语法错误。
xml的样本
<command>
<create>
<domain:create xmlns:domain="urn:ietf:params:xml:ns:domain-1.0"
xsi:schemaLocation="urn:ietf:params:xml:ns:domain-1.0
domain-1.0.xsd">
<domain:name>0000</domain:name>
<domain:period unit="y">2</domain:period>
<domain:ns>
<domain:hostObj>0000</domain:hostObj>
</domain:ns>
<domain:registrant>0000</domain:registrant>
<domain:contact type="tech">000</domain:contact>
<domain:contact type="admin">000</domain:contact>
<domain:contact type="billing">000</domain:contact>
<domain:authInfo>
<domain:pw>000</domain:pw>
</domain:authInfo>
</domain:create>
</create>
<clTRID>ABC:ics-forth:1079691187887</clTRID>
</command>
</epp>
低于我的代码
$p2xml = new SimpleXmlElement($p2xmlf);
foreach ($p2xml->command->create as $entry2)
{
$namespaces = $entry2->getNameSpaces(true);
$dc = $entry2->children($namespaces['domain']);
$dc->create->name = $domain_fields['name'];
$dc->create->ns->hostObj = $domain_fields['ns1'];
$dc->create->ns->hostObj = $domain_fields['ns2'];
$dc->create->registrant = $domain_fields['registrant'];
$dc->create->contact = $domain_fields['contact']; <-- problem here
$dc->create->contact = $domain_fields['contact']; <-- problem here
$dc->create->contact = $domain_fields['contact']; <-- problem here
$dc->create->authInfo->pw = $domain_fields['pw'];
$oxml = $p2xml->asXML();
尝试用//联系[@type = ['tech']和其他一些替换联系人,但我被困了
答案 0 :(得分:0)
我不知道PHP API是否接受XPath,但如果确实如此,那么您希望用于选择三个元素中的每一个的XPath将是:
//contact[@type='tech'] //contact[@type='admin'] //contact[@type='billing']