Comma separated list using XPath

Discussion in 'XML' started by nikoromano@gmail.com, Nov 30, 2007.

  1. Guest

    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>
    , Nov 30, 2007
    #1
    1. Advertising

  2. wrote:
    > 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.


    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
    Joseph Kesselman, Nov 30, 2007
    #2
    1. Advertising

  3. "Joseph Kesselman" <> wrote in message
    news:47508444$1@kcnews01...
    > wrote:
    >> 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.



    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
    Dimitre Novatchev, Dec 1, 2007
    #3
  4. Joseph Kesselman wrote:
    > wrote:
    >> 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.


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


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Dec 1, 2007
    #4
  5. > /(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...

    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
    Joe Kesselman, Dec 1, 2007
    #5
  6. "Joe Kesselman" <> wrote in message
    news:...
    >> /(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;


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


    Cheers,
    Dimitre Novatchev
    Dimitre Novatchev, Dec 3, 2007
    #6
  7. Guest

    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>


    On Dec 2, 10:26 pm, "Dimitre Novatchev" <> wrote:
    > "Joe Kesselman" <> wrote in message
    >
    > news:...
    >
    > >> /(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;

    >
    > Yes, this was the code in the OP first post.
    >
    > Cheers,
    > Dimitre Novatchev
    , Dec 4, 2007
    #7
  8. > 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


    <> wrote in message
    news:...
    > 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>
    >
    >
    > On Dec 2, 10:26 pm, "Dimitre Novatchev" <> wrote:
    >> "Joe Kesselman" <> wrote in message
    >>
    >> news:...
    >>
    >> >> /(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;

    >>
    >> Yes, this was the code in the OP first post.
    >>
    >> Cheers,
    >> Dimitre Novatchev

    >
    Dimitre Novatchev, Dec 5, 2007
    #8
  9. Guest

    That's pretty cool. Thanks Dimitre.


    On Dec 4, 10:24 pm, "Dimitre Novatchev" <> wrote:
    > > 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
    >
    > <> wrote in message
    >
    > news:...
    >
    > > 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>

    >
    > > On Dec 2, 10:26 pm, "Dimitre Novatchev" <> wrote:
    > >> "Joe Kesselman" <> wrote in message

    >
    > >>news:...

    >
    > >> >> /(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;

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

    >
    > >> Cheers,
    > >> Dimitre Novatchev
    , Dec 11, 2007
    #9
  10. nader

    Joined:
    Apr 16, 2011
    Messages:
    1
    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
    nader, Apr 16, 2011
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Miles
    Replies:
    1
    Views:
    585
    J├╝rgen Exner
    May 28, 2004
  2. Peter Rilling

    Array to a comma Separated String

    Peter Rilling, Jul 8, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    491
  3. Robert
    Replies:
    3
    Views:
    423
    norseman
    Jul 10, 2008
  4. Michiel Overtoom
    Replies:
    1
    Views:
    322
    Niklas Norrthon
    Jul 22, 2008
  5. Christina
    Replies:
    26
    Views:
    457
    Dave Anderson
    Feb 2, 2004
Loading...

Share This Page