Help with XPATH

A

Anon

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
 
J

Joris Gillis

Tempore 20:52:27 said:
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>

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,
 
P

Peter Flynn

Anon said:
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 said:
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
 
T

Tjerk Wolterink

Peter said:
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>


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.
 
P

Peter Flynn

Tjerk said:
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
 

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

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top