I have a small part of a xml export from a cms called tridion and I would like to parse this information using php.
I tried using DOMDocument
and DOMXPath
to access the data but fail to retrieve the required information.
For example when I try to access the node title from my example data I don't get any result.
$xmlDoc = new DOMDocument();
$xmlDoc->load($xmlFilePath);
$xpath = new DOMXPath($xmlDoc);
$xpath->registerNamespace('tcm', 'http://www.tridion.com/ContentManager/5.0');
$xpath->registerNamespace('xmlns', 'http://www.w3.org/1999/xlink');
$result = $xpath->query('title');
I believe this is a namespace issue but I don't really understand how to handle it.
This is what the export files look like (somewhat shortened for readability):
<PackageItem xmlns:tcm="http://www.tridion.com/ContentManager/5.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.sdltridion.com/ContentManager/ImportExport/Package/2013">
<PrimaryBlueprintParentUrl>/webdav/Content%20%28en%29/Content/120_external%20Links/Services/EL_www%2some-domin%2Ecom.xml</PrimaryBlueprintParentUrl>
<Data>
<tcm:Data>
<tcm:Title>EL_www.some-domain.com</tcm:Title>
<tcm:Type>Normal</tcm:Type>
<tcm:Schema xlink:type="simple" xlink:title="External Link (EL)" xlink:href="/webdav/Content/System/Schemas/Common/External%20Link%20%28EL%29.xsd" IsMandatory="false" />
<tcm:Content>
<externallink xmlns="uuid:D612E2C9-CD2E-4CD8-9FAE-3826311343A8">
<title>www.some-domain.com</title>
<url xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.some-domain.com" />
</externallink>
</tcm:Content>
</tcm:Data>
</Data>
</PackageItem>
The <externallink>
element just before it defines the default namespace for it and <title>
element as xmlns="uuid:D612E2C9-CD2E-4CD8-9FAE-3826311343A8"
. So if you define this namespace (I just use a dummy one - d
) and then use this in your expression...
$xpath->registerNamespace('d', "uuid:D612E2C9-CD2E-4CD8-9FAE-3826311343A8");
$result = $xpath->query('//d:title');
Update...
For the url...
$result = $xpath->query('//d:url');
echo $xmlDoc->saveXML($result[0]);
Gives...
<url xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.some-domain.com"/>
As this doesn't have a value as such (I've just said to output the XML of the first node found), not sure what you need out of it.
If you just want the href attribute...
echo $result[0]->getAttribute("xlink:href");