I’m stumped. I have the following XML in a file:
<?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>
Using SimpleXML in PHP, I have the following code:
$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>";
I get the following output:
Notice: Undefined offset: 0 in file.php on line 105 Notice: Undefined
offset: 0 in file.php on line 108
123 Street
Buffalo
NY: New York
12345
Line 105 referencing $rawContactFirstName[0] and Line 108 referencing $rawContactLastName[0]
The first and last name is in the document twice and I’m referencing last name from the first occurrence and first name from the second occurrence because of the difference in the way that the globLib namespace is referenced.
I cannot get any information from any of the elements using the globLib namespace and I don’t understand why. I can get any other piece of information from the document as long as it’s not using the globLib namespace.
$xml->xpath(); should return an array. Consider the following:
print_r($rawContactFirstName);
Output:
Array ( )
var_dump($rawContactFirstName);
var_dump($rawContactFirstName[0]);
Output:
array(0) { }
I don’t understand how all of the other namespaces can return data without issue except this one using the exact same technique. According to http://validator.w3.org/ the XML document is valid.
Any ideas?
Try changing this line:
to:
(or whatever unique prefix you’d like to use).
And then reference it as such in your xpath:
I think what’s happening is that that the prefix “globLib” is already registered with a different URI in the document element, and that’s somehow confusing
registerXPathNamespace.As a side note, the XML you posted doesn’t appear to be valid. It’s got a bunch of unclosed tags and tags that are “closed” with opening tags – so I’d also make absolutely sure that whatever XML you’re using is valid.