From: "Silvain Piree" <[EMAIL PROTECTED]> > > I'm wondering about solving this problem in a slightly different way, by > > providing an XPath extension function. > > That would be a nice alternative solution! Would even be better > than my suggestion. > > Except for my request for an numericAttributeValue() method, > which isn't covered by your solution.
Though you could use Number n = element.numberValueOf( "@foo" ); Or to specify a default if the attribute is not specified you could do... Number n = element.numberValueOf( "@foo | 0.5" ); So you get an easy way to extract numerical attributes and you can specify a default as well! > > Since if the first XPath expression doesn't return anything the second one > > will be used. This is nice as it can be arbitrarily long such as > > But what if the first XPath expression does return a value, will the > end result not contain both values (instead of only the first)? No, it will just return the number/string/node for the first expression. Incidentally I added some test cases to the XPath test harness at dom4j/xml/test/xpath/tests.xml to prove to myself this all works fine. The reason is that the valueOf(), numberValueOf() and selectSingleNode() methods all find the first node and use that. So the | union operator works fine in this situation when finding Strings, numbers or the first node in a selection. If the first expression finds one or more nodes, the first one will be used, otherwise the second expression will be used and so on. So.... String text = element.valueOf( "@a | @b | @c | 'none'" ); would only return the 'a' attribute, or 'b', or 'c' or 'none' etc. However it would not work when using this code... List list = element.selectNodes( "/foo/a | /foo/b" ); since the selectNodes() always returns the whole XPath result set and so would union together both result sets. James _________________________________________________________ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com _______________________________________________ dom4j-user mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/dom4j-user
