Comma separated list using XPath

N

nikoromano

Can anyone think of a changing these XPath expressions to return comma
separated lists?

/Library/Branch/book/(if(author="John") then position() else "")
output: 1 3 5
desired output: 1, 3, 5

/Library/Branch/book/(if(author="Mary") then position() else "")
output: 2
desired output: 2

/Library/Branch/book/(if(author="Mark") then position() else "")
output: 4
desired output: 4

<Library>
<Branch>
<book>
<author>John</author>
</book>
<book>
<author>Mary</author>
</book>
</Branch>
<Branch>
<book>
<author>John</author>
</book>
<book>
<author>Mark</author>
</book>
<book>
<author>John</author>
</Branch>
</Library>
 
J

Joseph Kesselman

Can anyone think of a changing these XPath expressions to return comma
separated lists?

XPath itself just retrieves information; formatting it is someone else's
job. You'd want something more like XSLT or XQuery, which can generate
new structure around the extracted data. Or you'd need to invoke the
XPath from a bit of code which generated the appropriate wrapper text.
 
D

Dimitre Novatchev

Joseph Kesselman said:
XPath itself just retrieves information; formatting it is someone else's
job. You'd want something more like XSLT or XQuery, which can generate new
structure around the extracted data. Or you'd need to invoke the XPath
from a bit of code which generated the appropriate wrapper text.


Not quite so ...

The evaluation of this expression:

"/Library/Branch/book
/(if(author='John')
then concat(position(),
if(not(position() eq last()))
then ','
else ''
)
else ()
)"

produces the wanted results:

1, 3, 5


Cheers,
Dimitre Novatchev
 
M

Martin Honnen

Joseph said:
XPath itself just retrieves information; formatting it is someone else's
job.

No longer quite true for XPath 2.0 which the original poster seemed to
use anyway.
 
J

Joe Kesselman

/(if(author='John')

I believe if/then/else expressions require XPath 2.0 or XQuery. If
that's what you have available, go for it; if you're using 1.0...
 
D

Dimitre Novatchev

Joe Kesselman said:
I believe if/then/else expressions require XPath 2.0 or XQuery. If that's
what you have available, go for it;

Yes, this was the code in the OP first post.


Cheers,
Dimitre Novatchev
 
N

nikoromano

Hi Guys

Thanks for all the input. Dimitre, your suggestion is something I had
tried as well. Unfortunately I don't think it quite works... Well,
more specially it *does* work for the one example I posted, but
imagine if I added another book below the last one with the author
Mary. (I put an updated XML snippet below) The the output for John is:
1, 3, 5,
(Since the 5th book is no longer the last book overall, a comma is
added after the 5.)

The reason I am trying to use XPath is I am being forced to use Altova
StyleVision for this project, and as far as I am aware, StyleVision
won't let me do this any other way (i.e. using the XLST or XQuery).

After I posted this, I realized the problem is actually more
complicated than I had previously thought. The point is actually to
create a detailed list of books, sorted by title (title is just
another element inside book) with a summary on top telling which books
below correspond to the given authors by the position the book appears
in the report. I put an example below. So I don't know if this is
going to be possible. If anyone has any other thoughts, either for
the first problem as an intellectual exercise, or to the 'revised'
situation--including using XSLT or XQuery--that'd be great. Otherwise
thanks for all the input!

Output example
Books by John: 1, 2, 5
Books by Mary: 3, 6
Books by Mark: 4

Book 1:
Title: A Book Title Starting With 'A'
Author: John

Book 2:
Title: Books Are Fun!
Author: John

Book 3:
Title: Can You Tell Me How To Get To Sesame Street?
Author Mary
....



XML Example
<?xml version="1.0" encoding="UTF-8"?>
<Library>
<Branch>
<book>
<author>John</author>
</book>
<book>
<author>Mary</author>
</book>
</Branch>
<Branch>
<book>
<author>John</author>
</book>
<book>
<author>Mark</author>
</book>
<book>
<author>John</author>
</book>
<book>
<author>Mary</author>
</book>
</Branch>
</Library>
 
D

Dimitre Novatchev

I put an example below. So I don't know if this is
going to be possible. If anyone has any other thoughts, either for
the first problem as an intellectual exercise, or to the 'revised'
situation--including using XSLT or XQuery--that'd be great. Otherwise
thanks for all the input!

Of course, this is possible. Below is one XPath expression the evaluation of
which produces the wanted result:

for $name in distinct-values(/*/*/*/author)
return
(concat('Books by ', $name, ': ',
string-join(
/Library/Branch/book
/(if(author=$name)
then concat(position(),
if(following::author[. eq $name])
then ','
else ''
)
else ()
),
''
),
'
'
)
)



When evaluated with the current document the one supplied in your last
message, the result is:


Books by John: 1,3,5
Books by Mary: 2,6
Books by Mark: 4



Cheers,
Dimitre Novatchev
 
N

nikoromano

That's pretty cool. Thanks Dimitre.


I put an example below. So I don't know if this is
going to be possible. If anyone has any other thoughts, either for
the first problem as an intellectual exercise, or to the 'revised'
situation--including using XSLT or XQuery--that'd be great. Otherwise
thanks for all the input!

Of course, this is possible. Below is one XPath expression the evaluation of
which produces the wanted result:

for $name in distinct-values(/*/*/*/author)
return
(concat('Books by ', $name, ': ',
string-join(
/Library/Branch/book
/(if(author=$name)
then concat(position(),
if(following::author[. eq $name])
then ','
else ''
)
else ()
),
''
),
'
'
)
)

When evaluated with the current document the one supplied in your last
message, the result is:

Books by John: 1,3,5
Books by Mary: 2,6
Books by Mark: 4

Cheers,
Dimitre Novatchev


Thanks for all the input. Dimitre, your suggestion is something I had
tried as well. Unfortunately I don't think it quite works... Well,
more specially it *does* work for the one example I posted, but
imagine if I added another book below the last one with the author
Mary. (I put an updated XML snippet below) The the output for John is:
1, 3, 5,
(Since the 5th book is no longer the last book overall, a comma is
added after the 5.)
The reason I am trying to use XPath is I am being forced to use Altova
StyleVision for this project, and as far as I am aware, StyleVision
won't let me do this any other way (i.e. using the XLST or XQuery).
After I posted this, I realized the problem is actually more
complicated than I had previously thought. The point is actually to
create a detailed list of books, sorted by title (title is just
another element inside book) with a summary on top telling which books
below correspond to the given authors by the position the book appears
in the report. I put an example below. So I don't know if this is
going to be possible. If anyone has any other thoughts, either for
the first problem as an intellectual exercise, or to the 'revised'
situation--including using XSLT or XQuery--that'd be great. Otherwise
thanks for all the input!
Output example
Books by John: 1, 2, 5
Books by Mary: 3, 6
Books by Mark: 4
Book 1:
Title: A Book Title Starting With 'A'
Author: John
Book 2:
Title: Books Are Fun!
Author: John
Book 3:
Title: Can You Tell Me How To Get To Sesame Street?
Author Mary
...
XML Example
<?xml version="1.0" encoding="UTF-8"?>
<Library>
<Branch>
<book>
<author>John</author>
</book>
<book>
<author>Mary</author>
</book>
</Branch>
<Branch>
<book>
<author>John</author>
</book>
<book>
<author>Mark</author>
</book>
<book>
<author>John</author>
</book>
<book>
<author>Mary</author>
</book>
</Branch>
</Library>
 
Joined
Apr 16, 2011
Messages
1
Reaction score
0
HI guys;
I've question about extraction specific information for example:
If I have (Mickle,is a student in a college)
How can I extract the Name (Mickle) without including the description (is a student in a college) Using Xpath or XSLT (ANY WAY) FROM XML document...

Please Answer me ASAP.

ThankX
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top