我很难过。我在文件中有以下XML:
<?xml version="1.0" encoding="UTF-8"?>
<grant:GrantApplication xmlns:grant="http://apply.grants.gov/system/MetaGrantApplication" xmlns:globLib="http://apply.grants.gov/system/GlobalLibrary-V1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://apply.grants.gov/system/MetaGrantApplication http://apply07.grants.gov/apply/opportunities/schemas/agency/oppEPA-R5-GL2011-1-cfda66.469.xsd">
<grant:Forms>
<EPA_KeyContacts:KeyContactPersons xmlns:EPA_KeyContacts="http://apply.grants.gov/forms/EPA_KeyContacts-V1.1" EPA_KeyContacts:FormVersion="1.1">
<EPA_KeyContacts:AuthorizedRepresentative>
<globLib:Name xmlns:globLib="http://apply.grants.gov/system/GlobalLibrary-V2.0">
<globLib:FirstName>Jane</globLib:FirstName>
<globLib:LastName>Doe</globLib:LastName>
</globLib:Name>
</EPA_KeyContacts:AuthorizedRepresentative>
</EPA_KeyContacts:KeyContactPersons>
<EPA4700_4_2_0:EPA4700_4_2_0 xmlns:EPA4700_4_2_0="http://apply.grants.gov/forms/EPA4700_4_2_0-V2.0" EPA4700_4_2_0:FormVersion="2.0">
<EPA4700_4_2_0:ApplicantInfo>
<EPA4700_4_2_0:ApplicantName>ABC 123</EPA4700_4_2_0:ApplicantName>
<EPA4700_4_2_0:ApplicantAddress>
<EPA4700_4_2_0:Address>123 Street</EPA4700_4_2_0:Address>
<EPA4700_4_2_0:City>Buffalo</EPA4700_4_2_0:City>
<EPA4700_4_2_0:State>NY: New York</EPA4700_4_2_0:State>
<EPA4700_4_2_0:ZipCode>12345</EPA4700_4_2_0:ZipCode>
</EPA4700_4_2_0:ApplicantAddress>
<EPA4700_4_2_0:ApplicantInfo>
<EPA4700_4_2_0:EPA4700_4_2_0 xmlns:EPA4700_4_2_0="http://apply.grants.gov/forms/EPA4700_4_2_0-V2.0" EPA4700_4_2_0:FormVersion="2.0">
<SF424_2_1:SF424_2_1 xmlns:SF424_2_1="http://apply.grants.gov/forms/SF424_2_1-V2.1" SF424_2_1:FormVersion="2.1">
<SF424_2_1:ContactPerson>
<globLib:FirstName xmlns:globLib="http://apply.grants.gov/system/GlobalLibrary-V2.0">Jane</globLib:FirstName>
<globLib:LastName xmlns:globLib="http://apply.grants.gov/system/GlobalLibrary-V2.0">Doe</globLib:LastName>
<SF424_2_1:ContactPerson>
<SF424_2_1:SF424_2_1>
</grant:Forms>
</grant:GrantApplication>
在PHP中使用SimpleXML,我有以下代码:
$xml = simplexml_load_file($appdir);
$xml->registerXPathNamespace("grant","http://apply.grants.gov/system/MetaGrantApplication");
$xml->registerXPathNamespace("header","http://apply.grants.gov/system/Header-V1.0");
$xml->registerXPathNamespace("globLib","http://apply.grants.gov/system/GlobalLibrary-V2.0");
$xml->registerXPathNamespace("EPA_KeyContacts","http://apply.grants.gov/forms/EPA_KeyContacts-V1.1");
$xml->registerXPathNamespace("SF424A","http://apply.grants.gov/forms/SF424A-V1.0");
$xml->registerXPathNamespace("SF424_2_1","http://apply.grants.gov/forms/SF424_2_1-V2.1");
$xml->registerXPathNamespace("EPA4700_4_2_0","http://apply.grants.gov/forms/EPA4700_4_2_0-V2.0");
$rawContactFirstName = $xml->xpath("/grant:GrantApplication/grant:Forms/SF424_2_1:SF424_2_1/SF424_2_1:ContactPerson/globLib:FirstName");
$contactFirstName = $rawContactFirstName[0];
$rawContactLastName = $xml->xpath("/grant:GrantApplication/grant:Forms/EPA_KeyContacts:KeyContactPersons/EPA_KeyContacts:AuthorizedRepresentative/globLib:Name/globLib:LastName");
$contactLastName = $rawContactLastName[0];
$rawStreetAddress = $xml->xpath("/grant:GrantApplication/grant:Forms/EPA4700_4_2_0:EPA4700_4_2_0/EPA4700_4_2_0:ApplicantInfo/EPA4700_4_2_0:ApplicantAddress/EPA4700_4_2_0:Address");
$streetAddress = $rawStreetAddress[0];
$rawCity = $xml->xpath("/grant:GrantApplication/grant:Forms/EPA4700_4_2_0:EPA4700_4_2_0/EPA4700_4_2_0:ApplicantInfo/EPA4700_4_2_0:ApplicantAddress/EPA4700_4_2_0:City");
$city = $rawCity[0];
$rawState = $xml->xpath("/grant:GrantApplication/grant:Forms/EPA4700_4_2_0:EPA4700_4_2_0/EPA4700_4_2_0:ApplicantInfo/EPA4700_4_2_0:ApplicantAddress/EPA4700_4_2_0:State");
$state = $rawState[0];
$rawZip = $xml->xpath("/grant:GrantApplication/grant:Forms/EPA4700_4_2_0:EPA4700_4_2_0/EPA4700_4_2_0:ApplicantInfo/EPA4700_4_2_0:ApplicantAddress/EPA4700_4_2_0:ZipCode");
$zip = $rawZip[0];
echo $contactFirstName."<br>".
$contactLastName."<br>".
$streetAddress."<br>".
$city."<br>".
$state."<br>".
$zip."<br>";
我得到以下输出:
注意:未定义的偏移量:第105行的file.php中的0注意:未定义 offset:第108行的file.php中的0
123 Street
布法罗
NY:纽约
12345
第105行引用$ rawContactFirstName [0]和第108行引用$ rawContactLastName [0]
第一个和最后一个名字在文档中两次,我引用了第一次出现的姓氏和第二次出现的名字,因为globLib名称空间的引用方式不同。
我无法使用globLib命名空间从任何元素获取任何信息,我不明白为什么。只要不使用globLib命名空间,我就可以从文档中获取任何其他信息。
$ XML-&GT;的xpath();应该返回一个数组。请考虑以下事项:
print_r($rawContactFirstName);
输出: 数组()
var_dump($rawContactFirstName);
var_dump($rawContactFirstName[0]);
输出: array(0){}
我不明白所有其他命名空间如何在没有问题的情况下返回数据,除非使用完全相同的技术。根据{{3}},XML文档是有效的。
有什么想法吗?
答案 0 :(得分:0)
尝试更改此行:
$xml->registerXPathNamespace("globLib","http://apply.grants.gov/system/GlobalLibrary-V2.0");
为:
$xml->registerXPathNamespace("globLib2","http://apply.grants.gov/system/GlobalLibrary-V2.0");
(或您想要使用的任何唯一前缀)。
然后在你的xpath中引用它:
$rawContactFirstName = $xml->xpath("/grant:GrantApplication/grant:Forms/SF424_2_1:SF424_2_1/SF424_2_1:ContactPerson/globLib2:FirstName");
我认为正在发生的事情是前缀“globLib”已经在文档元素中注册了不同的URI,这让registerXPathNamespace
混淆了。
作为旁注,您发布的XML似乎无效。它有一堆未封闭的标签和标签,用开放标签“关闭” - 所以我也绝对确保你使用的 有效的XML。