Monday, 12 August 2013

How to select all elements that have a certain attribute, regardless of value, via an xpath query?

How to select all elements that have a certain attribute, regardless of
value, via an xpath query?

I have an XML file loaded into the PHP DOM manipulation system something
like this:
$xml = file_get_contents('something.xml');
$dom = new DOMDocument();
$dom->loadXML($xml);
In the XML files are various elements with XMLNS tags similar to the
following:
<email xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:type="simple">example&commat;email.com</email>
<graphic xlink:href="example-image.tif" alt-version="no" mimetype="image"
position="float" xlink:type="simple"></graphic>
I'd like to strip out attributes such as "xmlns:xlink" and "xlink:type"
without removing the elements.
Is there an xpath query that will return every element with an attribute
name = "xmlns:xlink" or "xlink:type" or... whatever?
I've tried things like this:
$xpath->query('//*[xmlns:xlink]')
or
$xpath->query('//*[xmlns:xlink=*]')
But those don't work. I haven't really been able to find a good reference
for xpath, so some decent reading as well as an answer to the question
would be very welcome.

No comments:

Post a Comment