Thread: getAttribute()
Brought to you by:
bs_php,
nigelswinson
From: Philipp L. <le...@hi...> - 2002-04-19 07:39:56
|
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. 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 ). 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; } |
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 |
From: Philipp L. <le...@hi...> - 2002-05-10 00:29:26
|
From: "Nigel Swinson" <nig...@us...> > > 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? > It's nothing critical for me. Of course I validate my XML but usually just in developing the application, writing test data manually and programmatically, and then I have other tools available. I think the top priority is speed (I didn't check out the new debugged 3.0 version yet -- as I mentioned, the older 3.0 failed with my PHP forum -- but I was really happy to hear it's apparently so much faster). Of course it's a tiny problem that e.g. DTD default attribute values (if the attribute is omitted in the XML) aren't understood, because this means I have to replicate those values in the PHP in some way. >.. > 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. No, somehow I remember it returning an *error* (script ended) when the attribute didn't exist (version 2.2) -- I didn't even get the chance to assign the return value to a variable to check it for being empty/ set (I test on Windows PHP/ PWS). |
From: Peter R. <php...@pe...> - 2002-05-10 09:52:57
|
On Friday 10 May 2002 1:11, Nigel Swinson wrote: > 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? it's a nice idea to be able to get default values out of a dtd, but surely the way to go to do this is to use xsd and not dtd. Then we can use phpxpath as it stands to get the appropriate default out of the xsd; phpxpath itself would not need to be changed - I can imagine the changes needed to use dtds would be substantial. |