Is this only possible in XPath 2.0  
Author Message
we7313





PostPosted: XML and the .NET Framework, Is this only possible in XPath 2.0 Top

I can't fugure out how to write an xpath to do the following in xpath 1.0:

Doc

<Plane>

<Seats Number="1" Window="No"/>

<Seats Number="2" Window="Yes"/>

</Plane>

Desired Results:

1^NO

2^Yes

The problem is if I use concat in xpath 1.0 I only get a single string result.

Any ideas




.NET Development28  
 
 
Sinan Ussakli - MSFT





PostPosted: XML and the .NET Framework, Is this only possible in XPath 2.0 Top

Hi,

The following XPath command will return you a string that contains the first element's number and window concataneted.

When you use concat, it return only stringtype, therefore it's not possible to navigate over it. There might be a hack to get around of this but I'm not aware of such thing.



 
 
we7313





PostPosted: XML and the .NET Framework, Is this only possible in XPath 2.0 Top

Right, that is the problem I need multiple node set result.

Concat with only return a single string...



 
 
Dimitre_Novatchev - MSFT





PostPosted: XML and the .NET Framework, Is this only possible in XPath 2.0 Top

This is easy if the number of "Seats" elements is known (or can be obtained and then the XPath expression can be dynamically generated.

In case one knows an expected maximum number of "Seats" elements, it is still possible to use an XPath expression like the following:

concat(

substring('^', 1, not(1 > count(/*/*))),



substring('^', 1, not(2 > count(/*/*))),



substring('^', 1, not(3 > count(/*/*))),



substring('^', 1, not(4 > count(/*/*))),



substring('^', 1, not(5 > count(/*/*))),



substring('^', 1, not(6 > count(/*/*))),



substring('^', 1, not(7 > count(/*/*))),



substring('^', 1, not(8 > count(/*/*))),



substring('^', 1, not(9 > count(/*/*))),



substring('^', 1, not(10 > count(/*/*))),



substring('^', 1, not(11 > count(/*/*))),


)

When this XPath expression is evaluated on the xml document below:

<Plane>

<Seats Number="1" Window="No"/>

<Seats Number="2" Window="Yes"/>

<Seats Number="3" Window="No"/>

<Seats Number="4" Window="Yes"/>

</Plane>

the wanted result will be produced:

1^No
2^Yes
3^No
4^Yes

Cheers,
Dimitre Novatchev


 
 
we7313





PostPosted: XML and the .NET Framework, Is this only possible in XPath 2.0 Top

<Plane>

<Seats Number="512" Window="No"/>

<Seats Number="322" Window="Yes"/>

<Seats Number="122" Window="No"/>

<Seats Number="340" Window="Yes"/>

</Plane>

This might be used to represent inventory availability.

The result must be able to combine the 2 attributes in result node sets.



 
 
Dimitre_Novatchev - MSFT





PostPosted: XML and the .NET Framework, Is this only possible in XPath 2.0 Top

<Plane>

<Seats Number="512" Window="No"/>

<Seats Number="322" Window="Yes"/>

<Seats Number="122" Window="No"/>

<Seats Number="340" Window="Yes"/>

</Plane>

This might be used to represent inventory availability.

The result must be able to combine the 2 attributes in result node sets.

In the solution provided, the "Number" attribute is not used at all, so no assumption is being made that it will denote something specific.

The solution works for your last example, producing, as required:

512^No
322^Yes
122^No
340^Yes

Cheers,
Dimitre Novatchev


 
 
-Anton Lapounov





PostPosted: XML and the .NET Framework, Is this only possible in XPath 2.0 Top

I guess this may be simplified to

translate(concat(











), ' ', '^')


 
 
we7313





PostPosted: XML and the .NET Framework, Is this only possible in XPath 2.0 Top

The problem is that I need the results to come back in seperate node sets. I don't need a way to add a line return to make it look like it multiple node sets.

We have an application that takes in an xpath and executes it against a doc. If the results have multiple node results it seeds them in the database with a sequence. Every example provided so far only returns a single string. I need a 1 to many node set.



 
 
Dimitre_Novatchev - MSFT





PostPosted: XML and the .NET Framework, Is this only possible in XPath 2.0 Top

An XPath (1.0 or 2.0) expression cannot "create node-sets" -it can select a set of nodes that exist on the document(s) against which the XPath expression is evaluated.

Also, it is not at all clear what you mea by "concatinate node-sets" In XPath concatenation is an operation defined on two strings, not on node-sets. Probably you meant the union operation But it doesn't produce two node-sets -- just one node-set, which is the union of the two arguments passed to the union operator.

An XPath 2.0 can create a sequence of nodes (or other items), which is very different from a node-set (for example, the same node my occur more than once in a sequence).

Therefore, could you, please, provide a more precise definition of your problem -- a complete small example would be most appropriate.

Cheers,
Dimitre Novatchev.


 
 
we7313





PostPosted: XML and the .NET Framework, Is this only possible in XPath 2.0 Top

Ok I'll try again:

example:

XML

<Plane>

<Seats Number="512" Window="No"/>

<Seats Number="322" Window="Yes"/>

<Seats Number="122" Window="No"/>

<Seats Number="340" Window="Yes"/>

</Plane>

The following xpath would return multiple nodes.

/Plane/Seats

I do not want a string returned.

So that pretty much sums up the problem. I can do this in xpath 2.0.



 
 
Dimitre_Novatchev - MSFT





PostPosted: XML and the .NET Framework, Is this only possible in XPath 2.0 Top

Yes,

however you confused everybody by using a phrase like "(except stringing them together)".

What you actually wanted is by only using a single XPath expression to create a single text node out of two attribute nodes and to return the sequence of the nodes, produced in this way.

This is possible in XPath 2.0, because of the "for" expression.

One could use XSLT 1.0 for this purpose, or one could code an extension function and make it accessible in the XPath evaluation context, that would create the new text node.

Cheers,
Dimitre Novatchev