473,785 Members | 2,325 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>Gulliver s 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>Gulliver s 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 1729
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>Gulliver s 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:styleshe et 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('bo ok','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>Gulliver s 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>Gulliver s 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>Gulliver s 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>Gulliver s 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
6351
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 method would behave like the 'findnodes' XML method in Perl. Namely that you can pass it an xpath statement and it will find nodes that match: $array = $node->get_elements_by_tagname($xpath); This is long so here's a pagebreak: And, indeed,...
0
1930
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
2411
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 Thierry
0
1728
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" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="Loan"> <xs:complexType> <xs:sequence> <xs:element name="Borrower" maxOccurs="unbounded"> <xs:complexType> <xs:attribute name="BorrID" use="required">
4
2507
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 of projects. Therefore, I wish to include the project customType in the projectCollection namespace. I therefore have declared two xsd documents: project.xsd and projectcollection.xsd
6
15511
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
1291
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 namespace
7
1267
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
416
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 is necrotic and on the wrong side of the horse. </Description> <FileName>deadbowel.jpg</FileName> <FilePath>/photos/Pathology/deadbowel.jpg</FilePath> </Photo>
2
1792
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 this as I have received this XML and I didn't create it. All I want to is retrieve the PROJECTNAME from the following XML. I am typing this XML and XPATH into this site http://www.activsoftware.com/xml/xpath/ The XPATH that does not work is...
0
9481
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10155
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8979
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.