Re: getAttribute()
Brought to you by:
bs_php,
nigelswinson
From: Nigel S. <nig...@us...> - 2002-05-10 00:10:08
|
Don't seem to have a record of replying to this... > I put my little own getAttribute() function in the class. Did I miss > something or is there really only getAttributes(), and if there's no > attribute of that kind available it causes an error? That were my experience > with v2.20. Well I use a DTD to my XML that even expands some attributes to > a value if none is given but I guess DTDs aren't parsed. Version 3 only has a getAttributes() function, but it has an optional parameter that turns it into a getAttribute() function. DTDs aren't parsed. Have observed that this is a deficiency, wasn't sure how important it was to our users. Not sure how complex it would be to parse and use either. Comments? > Another question, how can/ should I comment the class now that I did a > little change? I tried to scan the mozilla license for some information. I > hope it's not illegal to redistribute the class inside my package (it's not > the core, I just use it for the free QML-interpreter at > http://questml.com ). Comment it? Well in the JavaDoc style: http://java.sun.com/j2se/javadoc/writingdoccomments/index.html and you should label yourself as the author using the @author tag. > My own function actually returns not null but an empty string if the > attribute is not found, which I think is not really standard but very nice > for my own purpoes. Now in case it's not a workaround for something that's > already in the class here goes my source (just as reference with no intend > to have it in any "official" version). > > function getAttribute($absoluteXPath, $attributeName) > { > $attributeValue = ""; > $attributes = $this->getAttributes($absoluteXPath); > foreach ($attributes as $key => $value) > { > if ($key == $attributeName && $attributeValue == "") > $attributeValue = $value; > } > > return $attributeValue; > } I think that the Php.XPath class should stick to returning an unset value if the attribute is not set. It probably makes for a more useful function. You can use the empty() and isset() functions to determine the state of the return value, but I agree that it might be a little annoying to use. What you could do is create a Php.XPath "wrapper" and overwrite the supplied getAttributes() function that handles errors and nulls in the way you want it too. I think that would be the best idea. ie. class MyXPath extends XPath { // My overridden version of getAttributes. Means return value is "" instead of null. function getAttributes($absoluteXPath, $attrName=NULL) { $ret = parent::getAttributes($absoluteXPath, $attrName); if (!$ret) return ""; return $ret; } } Cheers Nigel |