473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help with XPATH

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
Jul 20 '05 #1
4 1711
Tempore 20:52:27, die Sunday 09 January 2005 AD, hinc in foro {comp.text.xml} scripsit Anon <an**@anon.com>:
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:output 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-v...i?userid=38041)
"Et ipsa scientia potestas est" - Francis Bacon , Meditationes sacrae
Jul 20 '05 #2
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_
Jul 20 '05 #3
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.
Jul 20 '05 #4
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_
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: MegaZone | last post by:
I'm having some issues with PHP DOMXML - in particular the get_elements_by_tagname method. Now, the PGP docs on this are, well, sparse, so maybe I'm just doing something stupid. I thought this...
0
by: gael.pegliasco | last post by:
Hi, How are you dear and nice helper :) ? I'm trying to test xpath with this simple program : import xml.dom.minidom from xml.xpath.Context import Context import xml.xpath
3
by: Thierry Lam | last post by:
Let's say I have the following xml tag: <para role="success">1</para> I can't figure out what kind of python xml.dom codes I should invoke to read the data 1? Any help please? Thanks...
0
by: Rajesh Jain | last post by:
I Have 2 separate schemas. --------------Schema 1 is defined as below----------- <xs:schema targetNamespace="http://Schemas/1" xmlns="http://Schemas/1" xmlns:xs="http://www.w3.org/2001/XMLSchema"...
4
by: Iain A. Mcleod | last post by:
Hi I'm stuck with the following schema validation problem in VS.NET 2003: I have two types of xml document and related schema: project and projectCollection. A projectcollection is just a set...
6
by: Chua Wen Ching | last post by:
Hi there, I had this xml file with me (not yet consider implementing xml namespaces yet). <?xml version='1.0'?> <Object> <Windows> <EID>1</EID> <EDesc>Error 1</EDesc> </Windows>
3
by: John Lee | last post by:
Hi, I have a document <root> <name>John</name> </root> and the xpath "//name" will return correct node by using xmldoc.SelectSingleNode(xpath) and if the document contains a default...
7
by: Peter Morris [Droopy eyes software] | last post by:
Hi all I need a little help with XSD, here is my XML <data> <brandData> <brandGroups> <brandGroup> <name>Group1</name> </brandGroup>
1
by: Showjumper | last post by:
Hi, I need some help with xpath. I am very unfamilair with XPATH. I have an xml file with the folloing structure: <Photos> <Photo> <Description>This is a photo of horse that colicked. The bowel...
2
by: Monty | last post by:
Despite reading posts in Google, I don't understand XPATH. Can someone help me write an XPATH. From Google I think my problem is that the default namespace does not have a prefix. I can't change...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.