Help with XPATH

Discussion in 'XML' started by Anon, Jan 9, 2005.

  1. Anon

    Anon Guest

    Hi, to not deceive you this is a school project. We are developing some XSLT
    to format some data in HTML for Internet Explorer. The work uses XSLT and
    CSS and XML. The objective is to write a search form, the user enters
    details in a browser window, presses the "search" button, and books that
    match the search criteria are displayed in a table formatted with pretty
    colours.

    We have a problem.

    We thought the XML searched by the application looked like this:

    <BOOKS>
    <BOOK isbn="0-671-00174-4">
    <TITLE>Gullivers Travels</TITLE>
    </BOOK>
    <BOOK isbn="1-84466-725-1">
    <TITLE>Hansel and Gretel</TITLE>
    </BOOK>
    <BOOKS>


    To search, to display the book title in a table, we were using for example
    the following XPATH

    /BOOKS/BOOK[@isbn="0-671-00174-4"]/TITLE


    But we misunderstood things and using the sample data given found that the
    XML really looks like this:

    <BOOKS>
    <BOOK>
    <TITLE>Gullivers Travels</TITLE>
    <ISBN>0-671-00174-4</ISBN>
    </BOOK>
    <BOOK>
    <TITLE>Hansel and Gretel</TITLE>
    <ISBN>1-84466-725-1</ISBN>
    </BOOK>
    <BOOKS>

    Our problem is that we don't know the general type of XPATH now to use. And
    because we are panicking without the course notes are just guessing at all
    the possible obvious XPATH expressions and trying to find an example in
    Google

    Can someone please take pity on us and help wqith the one XPATH line
    necessary.

    Thank you
    Pete
     
    Anon, Jan 9, 2005
    #1
    1. Advertising

  2. Anon

    Joris Gillis Guest

    Tempore 20:52:27, die Sunday 09 January 2005 AD, hinc in foro {comp.text.xml} scripsit Anon <>:

    > To search, to display the book title in a table, we were using for example
    > the following XPATH
    >
    > /BOOKS/BOOK[@isbn="0-671-00174-4"]/TITLE
    >
    >
    > But we misunderstood things and using the sample data given found that the
    > XML really looks like this:
    >
    > <BOOKS>
    > <BOOK>
    > <TITLE>Gullivers Travels</TITLE>
    > <ISBN>0-671-00174-4</ISBN>
    > </BOOK>
    > <BOOK>
    > <TITLE>Hansel and Gretel</TITLE>
    > <ISBN>1-84466-725-1</ISBN>
    > </BOOK>
    > <BOOKS>

    Hi,

    The Xpath would be this:

    <xsl:value-of select="/BOOKS/BOOK[ISBN='0-671-00174-4']/TITLE"/>

    You could also use a key:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput indent="yes"/>

    <xsl:key name="book" match="BOOK" use="ISBN"/>

    <xsl:template match="/">
    <xsl:value-of select="key('book','0-671-00174-4')/TITLE"/>
    </xsl:template>

    </xsl:stylesheet>

    > To not deceive you this is a school project. We are developing some XSLTto format some data in HTML for Internet Explorer. The work uses XSLT andCSS and XML.


    That's a nice project. What grade are you in? I'm in the last year of middle school and there's no person on my school that has ever heard of XML...


    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    "Et ipsa scientia potestas est" - Francis Bacon , Meditationes sacrae
     
    Joris Gillis, Jan 9, 2005
    #2
    1. Advertising

  3. Anon

    Peter Flynn Guest

    Anon wrote:

    > Hi, to not deceive you this is a school project. We are developing some
    > XSLT to format some data in HTML for Internet Explorer.


    Don't. Make it work in any browser. This is much more impressive to your
    teachers, and much more like sensible. If the projects specifies IE, then
    do it, but make it work in (at least) Opera and Firefox also.

    > The work uses XSLT and CSS and XML.


    The CSS is the hard part. Microsoft's implementation is disastrously broken,
    with only some of it working. This makes it harder (but not impossible) to
    get it to work in all browsers.

    > The objective is to write a search form, the user enters
    > details in a browser window, presses the "search" button, and books that
    > match the search criteria are displayed in a table formatted with pretty
    > colours.


    OK, but you're going to need a script somewhere, either in Javascript in
    the document, or somewhere back on the server.

    > We have a problem.
    >
    > We thought the XML searched by the application looked like this:
    >
    > <BOOKS>
    > <BOOK isbn="0-671-00174-4">
    > <TITLE>Gullivers Travels</TITLE>
    > </BOOK>
    > <BOOK isbn="1-84466-725-1">
    > <TITLE>Hansel and Gretel</TITLE>
    > </BOOK>
    > <BOOKS>


    That looks good. Except that last end-tag should be </BOOKS>.

    > To search, to display the book title in a table, we were using for example
    > the following XPATH
    >
    > /BOOKS/BOOK[@isbn="0-671-00174-4"]/TITLE
    >
    >
    > But we misunderstood things and using the sample data given found that the
    > XML really looks like this:
    >
    > <BOOKS>
    > <BOOK>
    > <TITLE>Gullivers Travels</TITLE>
    > <ISBN>0-671-00174-4</ISBN>
    > </BOOK>
    > <BOOK>
    > <TITLE>Hansel and Gretel</TITLE>
    > <ISBN>1-84466-725-1</ISBN>
    > </BOOK>
    > </BOOKS>


    OK. The XPath you need is /BOOKS/BOOK[ISBN='0-671-00174-4']/TITLE
    There are several other ways but that one is probably the most efficient.
    /BOOKS/BOOK/ISBN[.='0-671-00174-4']/preceding-sibling::TITLE will work,
    but it's a bit cumbersome.

    Be aware that *neither* of them will work if the XML file is written in
    the degenerate form beloved of programmers who don't understand markup:

    <BOOKS>
    <BOOK>
    <TITLE>
    Gullivers Travels
    </TITLE>
    <ISBN>
    0-671-00174-4
    </ISBN>
    </BOOK>
    <BOOK>
    <TITLE>
    Hansel and Gretel
    </TITLE>
    <ISBN>
    1-84466-725-1
    </ISBN>
    </BOOK>
    </BOOKS>

    because the linebreaks before and after the data get interpreted as spaces
    during parsing, so " 0-671-00174-4 " is not the same as "0-671-00174-4".

    Check out XPathTester for doing this kind of thing (requires Java). I put a
    copy at http://www.silmaril.ie/xml/xpathtester_1_4_saxon.jar

    ///Peter
    --
    "The cat in the box is both a wave and a particle"
    -- Terry Pratchett, introducing quantum physics in _The Authentic Cat_
     
    Peter Flynn, Jan 9, 2005
    #3
  4. Peter Flynn wrote:
    > Anon wrote:
    >
    >
    >>Hi, to not deceive you this is a school project. We are developing some
    >>XSLT to format some data in HTML for Internet Explorer.

    >
    >
    > Don't. Make it work in any browser. This is much more impressive to your
    > teachers, and much more like sensible. If the projects specifies IE, then
    > do it, but make it work in (at least) Opera and Firefox also.
    >
    >
    >>The work uses XSLT and CSS and XML.

    >
    >
    > The CSS is the hard part. Microsoft's implementation is disastrously broken,
    > with only some of it working. This makes it harder (but not impossible) to
    > get it to work in all browsers.
    >
    >
    >>The objective is to write a search form, the user enters
    >>details in a browser window, presses the "search" button, and books that
    >>match the search criteria are displayed in a table formatted with pretty
    >>colours.

    >
    >
    > OK, but you're going to need a script somewhere, either in Javascript in
    > the document, or somewhere back on the server.
    >
    >
    >>We have a problem.
    >>
    >>We thought the XML searched by the application looked like this:
    >>
    >><BOOKS>
    >> <BOOK isbn="0-671-00174-4">
    >> <TITLE>Gullivers Travels</TITLE>
    >> </BOOK>
    >> <BOOK isbn="1-84466-725-1">
    >> <TITLE>Hansel and Gretel</TITLE>
    >> </BOOK>
    >><BOOKS>

    >
    >
    > That looks good. Except that last end-tag should be </BOOKS>.
    >
    >
    >>To search, to display the book title in a table, we were using for example
    >>the following XPATH
    >>
    >>/BOOKS/BOOK[@isbn="0-671-00174-4"]/TITLE
    >>
    >>
    >>But we misunderstood things and using the sample data given found that the
    >>XML really looks like this:
    >>
    >><BOOKS>
    >> <BOOK>
    >> <TITLE>Gullivers Travels</TITLE>
    >> <ISBN>0-671-00174-4</ISBN>
    >> </BOOK>
    >> <BOOK>
    >> <TITLE>Hansel and Gretel</TITLE>
    >> <ISBN>1-84466-725-1</ISBN>
    >> </BOOK>
    >></BOOKS>

    >
    >
    > OK. The XPath you need is /BOOKS/BOOK[ISBN='0-671-00174-4']/TITLE
    > There are several other ways but that one is probably the most efficient.
    > /BOOKS/BOOK/ISBN[.='0-671-00174-4']/preceding-sibling::TITLE will work,
    > but it's a bit cumbersome.
    >
    > Be aware that *neither* of them will work if the XML file is written in
    > the degenerate form beloved of programmers who don't understand markup:
    >
    > <BOOKS>
    > <BOOK>
    > <TITLE>
    > Gullivers Travels
    > </TITLE>
    > <ISBN>
    > 0-671-00174-4
    > </ISBN>
    > </BOOK>
    > <BOOK>
    > <TITLE>
    > Hansel and Gretel
    > </TITLE>
    > <ISBN>
    > 1-84466-725-1
    > </ISBN>
    > </BOOK>
    > </BOOKS>
    >
    > because the linebreaks before and after the data get interpreted as spaces
    > during parsing, so " 0-671-00174-4 " is not the same as "0-671-00174-4".
    >
    > Check out XPathTester for doing this kind of thing (requires Java). I put a
    > copy at http://www.silmaril.ie/xml/xpathtester_1_4_saxon.jar
    >
    > ///Peter


    Doesnt xpath support trimming? Then it it is just the specification of how to use
    the xml format. So linebreaks in an entry should not be a problem if you say so in a specification.
    It is just what you agree to. Ok the xml specificatoin says that all data (also linebreaks) are part of
    the value. But if (par example) you agree with others that BOOKS xml files like the ones above can have newlines
    and the one who processes it should trim it, then it is not a problem.
     
    Tjerk Wolterink, Jan 10, 2005
    #4
  5. Anon

    Peter Flynn Guest

    Tjerk Wolterink wrote:

    > Peter Flynn wrote:

    [snip]
    >> because the linebreaks before and after the data get interpreted as
    >> spaces during parsing,

    [...]
    >
    > Doesnt xpath support trimming? Then it it is just the specification of how
    > to use the xml format. So linebreaks in an entry should not be a problem
    > if you say so in a specification. It is just what you agree to. Ok the xml
    > specificatoin says that all data (also linebreaks) are part of the value.
    > But if (par example) you agree with others that BOOKS xml files like the
    > ones above can have newlines and the one who processes it should trim it,
    > then it is not a problem.


    So if we agree that I can send you broken XML and you will accept it, that's
    not a problem?

    Sure it's not...until we try to use the files in the real world.

    I don't recall if XPath specifies the suppression of leading and trailing
    white-space in mixed content or not. But deliberately breaking a spec just
    because it's inconvenient is probably A Bad Idea.

    ///Peter
    --
    "The cat in the box is both a wave and a particle"
    -- Terry Pratchett, introducing quantum physics in _The Authentic Cat_
     
    Peter Flynn, Jan 10, 2005
    #5
    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. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,976
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,418
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    529
  4. goog
    Replies:
    0
    Views:
    512
  5. Tjerk Wolterink

    XPath: efficiency in xpath expressions

    Tjerk Wolterink, Nov 13, 2004, in forum: XML
    Replies:
    1
    Views:
    1,645
    Richard Tobin
    Nov 13, 2004
Loading...

Share This Page