1

Is it possible to do the following by just using XPath? I would like to find a way to do this in 1.0 I don't think its possible in 1.0.

Example

<test>
  <title>title1</title>
  <title>title2</title>
  <title>title3</title>
  <creator>creator1</creator>
  <creator>creator2</creator>
</test>

I want the following output:

title1 creator1
title2 creator2
title3

I was thinking this might be more suited for xquery but I'm not sure as I have never used it. It would be a lot of work to add xquery to my current application and why I'm trying to avoid. The closest thing I have come up with for working was using

/test/title/fn:string-join( (text(), /test/creator/text()[position()]), ' ')

The issue being I get both creators in this example for all. I was wonder if there was some way to get the current position and set the position for creator? I know how to do this in xslt easily but that is not a option for me.

Jens Erat
  • 37,523
  • 16
  • 80
  • 96
Flips
  • 170
  • 1
  • 11
  • Check this question regarding string concatenation using XPath: http://stackoverflow.com/questions/1403971/xpath-to-return-string-concatenation-of-qualifying-child-node-values. – Rolando Isidoro May 13 '13 at 15:01

1 Answers1

0

This is not possible in XPath 1.0. In both XQuery (1.0 is fine) and XPath 2.0, you could do like this:

for $i in 1 to max((count(//title), count(//creator)))
return string-join((//title[$i], //creator[$i]), ' ')
Jens Erat
  • 37,523
  • 16
  • 80
  • 96
  • Thanks. Though this does cause everything to be one item rather then separate. – Flips May 13 '13 at 15:32
  • Just remove `string-join` and call `/data()` for every title and creator and you'll get the sequence of text nodes. If you want the tags in the output, just remove the `string-join` call. :) Please exactly specify the expected output next time you ask a question, then you will get exactly the answer you're looking for at the first time. – Jens Erat May 13 '13 at 15:35
  • Sorry I though I did. The example you gave gives back one result title1 creator1 title2 creator2 title3 . I just thought the new line was clear that I wanted the data separated or in saxen terms multiple XdmItems. Thanks again. – Flips May 13 '13 at 15:47
  • I just realized that you used multiple lines in the markdown sources. Have you been able to achieve exactly what you need? – Jens Erat May 13 '13 at 15:49
  • Yes I ended up using: for $i in 1 to max((count(/test/title), count(/test/creator))) return string-join((/test/title[$i]/text(), /test/creator[$i]/text()),' ') – Flips May 13 '13 at 16:10
  • I updated my answer to reflect the real question. You should be able to do without the `/text()` calls. – Jens Erat May 13 '13 at 16:18