Hi Silvain

My only fear would be API bloat as you could make the same argument about
the valueOf() method, the selectObject(), selectSingleNode() and
selectNodes(). Even the attribute() and element() methods too.

I'm wondering about solving this problem in a slightly different way, by
providing an XPath extension function.

e.g. imagine if there was a default() function which took two arguments

default($foo, 1234)

The function would return the first value, unless it was null or an empty
list otherwise the second value was returned.

Then we could use this same simple mechanism across elements, attributes,
text nodes or string and number expressions.

e.g.

default(@foo, 'N/A')

Would return the @foo attribute or if its undefined then return "N/A".

This extension function could also be used across other object models in
Jaxen, like SAX (maybe one day! ;-).

So in use in dom4j it'd look like this...

Number n = node.numberValueOf( "default(/foo/bar/amount, 0)" );
String text = node.valueOf( "default(/foo/@x, 'N/A')" );
Element e = (Element) node.selectSingleNode( "default( /foo/bar, /foo ) " );


Actually thinking more about this we might be able to just use the XPath
union operator for this.

e.g.

Number n = node.numberValueOf( "/foo/bar/amount | 12)" );
String text = node.valueOf( "/foo/@x | 'N/A'" );
Element e = (Element) node.selectSingleNode( " /foo/bar |  /foo  " );

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

/foo/@x | /foo/@y | /foo/@z | 'no coordinates at all'

The use of '|' does seem pretty compelling now, as it can be used right
throughout the dom4j API which uses XPath...

James
----- Original Message -----
From: "Silvain Piree" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, November 15, 2001 4:21 PM
Subject: [dom4j-user] suggestion for numberValueOf()


>
> Hi,
>
> it would be nice if the method numberValueOf() defined with
> Element, would allow a default value if the specified xpath doesn't
> return null result, e.g. numberValueOf(xpath, defaultval).
>
> (This is similar to the attributeValue() method).
>
> Also, it would be nice if there were a numberAttributeValue() method
> which converts the attribute value to an integer (with a default value
> if the attribute does not exist).
>
> Silvain
>
>
>
>
> _______________________________________________
> dom4j-user mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/dom4j-user
>


_________________________________________________________
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

Reply via email to