我很困惑。我在一个文件中有以下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>";我得到以下输出:
注意事项:未定义的偏移量: file.php中的0在第105行通知:未定义的偏移量:0在第108行的file.php中
123号街
水牛
纽约:纽约
12345
第105行引用$rawContactFirstName和第108行引用$rawContactLastName
第一个和第二个名字在文档中两次,由于引用globLib命名空间的方式不同,所以我从第一次事件中引用姓氏,从第二次事件中引用第一个名称。
我无法从使用globLib命名空间的任何元素获得任何信息,我也不明白为什么。只要文档不使用globLib命名空间,我就可以从文档中获取任何其他信息。
$xml->xpath();应该返回一个数组。请考虑以下几点:
print_r($rawContactFirstName);输出:Array ( )
var_dump($rawContactFirstName);
var_dump($rawContactFirstName[0]);输出:数组(0){ }
我不明白为什么所有其他名称空间都可以毫无问题地返回数据,除了使用完全相同的技术的这个名称空间。根据http://validator.w3.org/,XML文档是有效的。
有什么想法吗?
发布于 2012-05-01 21:47:12
试着改变这一行:
$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“已经在document元素中用不同的URI注册了,这在某种程度上混淆了registerXPathNamespace。
顺便提一句,您发布的XML似乎并不有效。它有一堆未关闭的标记和带有开始标记的“关闭”标记--所以我也绝对要确保您使用的是有效的。
https://stackoverflow.com/questions/10402112
复制相似问题