Discussion:
DOMDocument getElementsByAttribute ??
Michael A. Peters
2009-03-09 23:18:32 UTC
Permalink
Seems like such a function does not exist in php.
I can write my own function that does it using
DOMElement->hasAttribute() - but I'm not sure how to get an array of
every element in the DOM to test them for the attribute.

Any hints?

I'm sure it's simple, I'm just not seeing the function that does it.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Nathan Rixham
2009-03-09 23:58:37 UTC
Permalink
Post by Michael A. Peters
Seems like such a function does not exist in php.
I can write my own function that does it using
DOMElement->hasAttribute() - but I'm not sure how to get an array of
every element in the DOM to test them for the attribute.
Any hints?
I'm sure it's simple, I'm just not seeing the function that does it.
DOMXPath :)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Michael A. Peters
2009-03-10 00:12:55 UTC
Permalink
Post by Nathan Rixham
Post by Michael A. Peters
Seems like such a function does not exist in php.
I can write my own function that does it using
DOMElement->hasAttribute() - but I'm not sure how to get an array of
every element in the DOM to test them for the attribute.
Any hints?
I'm sure it's simple, I'm just not seeing the function that does it.
DOMXPath :)
I figured it out -

$document->getElementsByTagName("*");

seems to work just fine.

I do need to find out more about XPath - unfortunately reading the
examples that are out in the wild is troublesome because it seems 95% of
them involve the deprecated dom model from pre php 5, so to make sense
of them I would have to port the examples to php5 DOMDocument first.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Nathan Rixham
2009-03-10 00:24:13 UTC
Permalink
Post by Michael A. Peters
Post by Nathan Rixham
Post by Michael A. Peters
Seems like such a function does not exist in php.
I can write my own function that does it using
DOMElement->hasAttribute() - but I'm not sure how to get an array of
every element in the DOM to test them for the attribute.
Any hints?
I'm sure it's simple, I'm just not seeing the function that does it.
DOMXPath :)
I figured it out -
$document->getElementsByTagName("*");
seems to work just fine.
I do need to find out more about XPath - unfortunately reading the
examples that are out in the wild is troublesome because it seems 95% of
them involve the deprecated dom model from pre php 5, so to make sense
of them I would have to port the examples to php5 DOMDocument first.
Xpath is easier than most think.. for example

//p[@class='red']

that's all p tags with a class of "red"

infact.. http://www.w3schools.com/XPath/xpath_syntax.asp covers about
everything you'll need for normal stuff :)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Michael A. Peters
2009-03-10 00:41:35 UTC
Permalink
Post by Nathan Rixham
Post by Michael A. Peters
Post by Nathan Rixham
Post by Michael A. Peters
Seems like such a function does not exist in php.
I can write my own function that does it using
DOMElement->hasAttribute() - but I'm not sure how to get an array of
every element in the DOM to test them for the attribute.
Any hints?
I'm sure it's simple, I'm just not seeing the function that does it.
DOMXPath :)
I figured it out -
$document->getElementsByTagName("*");
seems to work just fine.
I do need to find out more about XPath - unfortunately reading the
examples that are out in the wild is troublesome because it seems 95%
of them involve the deprecated dom model from pre php 5, so to make
sense of them I would have to port the examples to php5 DOMDocument
first.
Xpath is easier than most think.. for example
that's all p tags with a class of "red"
infact.. http://www.w3schools.com/XPath/xpath_syntax.asp covers about
everything you'll need for normal stuff :)
What I'm doing is writing a filter that removes forbidden attributes -
such as the event attributes (onload, onmouseover, etc.) - the value of
the attribute of the attribute I could care less about.

There's probably an xpath way to get all the elements and then check
them for a specified attribute, but using the wildcard with the
getElementsByTagName also works.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Andrew Ballard
2009-03-10 14:37:48 UTC
Permalink
Post by Nathan Rixham
Post by Michael A. Peters
Post by Nathan Rixham
Post by Michael A. Peters
Seems like such a function does not exist in php.
I can write my own function that does it using
DOMElement->hasAttribute() - but I'm not sure how to get an array of every
element in the DOM to test them for the attribute.
Any hints?
I'm sure it's simple, I'm just not seeing the function that does it.
DOMXPath :)
I figured it out -
$document->getElementsByTagName("*");
seems to work just fine.
I do need to find out more about XPath - unfortunately reading the
examples that are out in the wild is troublesome because it seems 95% of
them involve the deprecated dom model from pre php 5, so to make sense of
them I would have to port the examples to php5 DOMDocument first.
Xpath is easier than most think.. for example
that's all p tags with a class of "red"
infact.. http://www.w3schools.com/XPath/xpath_syntax.asp covers about
everything you'll need for normal stuff :)
What I'm doing is writing a filter that removes forbidden attributes - such
as the event attributes (onload, onmouseover, etc.) - the value of the
attribute of the attribute I could care less about.
There's probably an xpath way to get all the elements and then check them
for a specified attribute, but using the wildcard with the
getElementsByTagName also works.
If your source is well-formed enough to use the DOM library, then you
could probably write an XSLT template that would give you exactly what
you want. You still need to understand xpath syntax, but I found a
post here that talks about what you are doing:

http://www.biglist.com/lists/xsl-list/archives/200404/msg00668.html

Andrew
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Andrew Ballard
2009-03-10 14:33:08 UTC
Permalink
Post by Nathan Rixham
Post by Michael A. Peters
Post by Nathan Rixham
Post by Michael A. Peters
Seems like such a function does not exist in php.
I can write my own function that does it using
DOMElement->hasAttribute() - but I'm not sure how to get an array of every
element in the DOM to test them for the attribute.
Any hints?
I'm sure it's simple, I'm just not seeing the function that does it.
DOMXPath :)
I figured it out -
$document->getElementsByTagName("*");
seems to work just fine.
I do need to find out more about XPath - unfortunately reading the
examples that are out in the wild is troublesome because it seems 95% of
them involve the deprecated dom model from pre php 5, so to make sense of
them I would have to port the examples to php5 DOMDocument first.
Xpath is easier than most think.. for example
that's all p tags with a class of "red"
infact.. http://www.w3schools.com/XPath/xpath_syntax.asp covers about
everything you'll need for normal stuff :)
Well, I thought so too. That was until I had to use xpath on a few
documents that used namespaces. Try this with a strict xhtml document
and all of a sudden your simple xpath query becomes something like
this:

//*[namespace-uri()='http://www.w3.org/1999/xhtml' and local-name() =
'p' and @class='red']

Although, for the original question, I believe something like this
would still work (unless the attributes themselves are namespaced):

//*[@someattribute!='']

Andrew
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...