tags:

views:

128

answers:

3

In XPath it is possible to convert an object to string using the string() function. Now I want to convert the string back to an object.

I do understand it is not possible in some cases (for example for elements), because some information was lost. But it should be possible for simple types, like int or boolean.

I know, for numbers I can use number() function, but I want general mechanism which will work for any simple type variable.

+2  A: 

Going to string is easy, because you've told it that you want a string.

Similarly, going to number is easy, because you've told it that you want a number.

But there is no generic way to say 'turn it back into x', because you haven't told it what x is.

(In other words, string() is like a cast like Java/C/C++/C# have. But there is no uncast.)

string() isn't an object serializer, so you can't deserialize.

Why do you want this? Perhaps there is another way of solving your problem.

lavinio
Sorry for the delays in response... What I acctually need is simple-type serialization (well, serialization is easy) and deserialization, but of course I know the actual type of variable I'm going to construct! :)It seems the problem is not about XPath either :) I've just used BPEL <copy><from>xpath</from><to>myvariable</to></copy> construction, where myvariable is of right type, and this works fine
javapowered
A: 

In no language (that I know of) you can cast A to B and then call a magical function that reverts it back to whatever it was before you casted it.

The process of converting some data type into something else is always an unidirectional one - you lose the information what type it was before. That's because the new data type has no way of storing what it was before.

So, what are you trying to do? I strongly suspect that you ask this question because you are tackling a problem from the wrong end.

Tomalak
Agree, but if you know the type, you can often cast back. for example: (Circle) figure.
javapowered
A: 

If your object $x is the number 1234, then string($x) will be the string "1234".

If your object $x is a nodeset of 1000 XML elements, the first one being

<wibble><wobble>1<ping/>2</wobble>34</wibble>

then string($x) will be the string "1234".

The function is not a bijection, you can't have an inverse as many different values map to the same string.

Pete Kirkham