473,408 Members | 1,867 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,408 software developers and data experts.

Help with simple XPATH

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 /PROJECTS/PROJECTNAME.

Thank you
Monty

<PROJECTS xmlns="http://www.companyname.com/project"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PROJECTNAME>
Project A
</PROJECTNAME>
<PROJECTBUDGET>
27000
</PROJECTBUDGET>
</PROJECTS>

Dec 30 '06 #1
2 1770
Monty wrote:
The XPATH that does not work is /PROJECTS/PROJECTNAME.
<PROJECTS xmlns="http://www.companyname.com/project"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PROJECTNAME>
Project A
</PROJECTNAME>

Those elements are in the namespace http://www.companyname.com/project
so for XPath to qualify element names in your expression you need to
bind a prefix to the namespace URI. Check your XPath API on how to do
that exactly, you can choose any prefix you like and you don't have to
change the XML source document, you just need to make sure your XPath
API knows that e.g. 'pf' is bound to http://www.companyname.com/project
and then you can use expressions alike
/pf:PROJECTS/pf:PROJECTNAME

Or as an alternative you could use e.g.
/*[namespace-uri() = 'http://www.companyname.com/project' and
local-name() = 'PROJECTS']/*[namespace-uri() =
'http://www.companyname.com/project' and local-name() = 'PROJECTNAME']
--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 30 '06 #2
Monty wrote:
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 /PROJECTS/PROJECTNAME.

Thank you
Monty

<PROJECTS xmlns="http://www.companyname.com/project"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PROJECTNAME>
Project A
</PROJECTNAME>
<PROJECTBUDGET>
27000
</PROJECTBUDGET>
</PROJECTS>
You have to supply namespace prefixes in xsl/xpath for default
namespaces in xml. It took a bit of fussing, but here's a solution:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml" >
<xsl:template xmlns:pn="http://www.companyname.com/project" match="/">
<xsl:apply-templates select="pn:PROJECTS/pn:PROJECTNAME" />
<xsl:apply-templates select="pn:PROJECTS/pn:PROJECTBUDGET" />
</xsl:template>

<xsl:template xmlns:pn="http://www.companyname.com/project"
match="pn:PROJECTNAME">
Project Name: <xsl:value-of select = "." /><br />
</xsl:template>

<xsl:template xmlns:pn="http://www.companyname.com/project"
match="pn:PROJECTBUDGET">
Budget: <xsl:value-of select = "." />
</xsl:template>

</xsl:stylesheet>

--

_____________________
Myron Turner
http://www.room535.org
http://www.bstatzero.org
http://www.mturner.org/XML_PullParser/
Jan 5 '07 #3

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...
1
by: Joshua Beall | last post by:
Hi All, I have a task that should be very simple but I'm running into trouble. All I want to do is query a document using XPath, and save the resulting XML in a string. Here's that I am trying...
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
2
by: Jerry O | last post by:
Hi - I am stuck on what might be a simple Xpath expression, I just can't visualize the query. I have a node set that can be the child of any of three nodes. I would like to test if a node...
4
by: gfrommer | last post by:
Hello everyone, I've been reading through a bunch of XPath tutorials and am confused by a couple items. First, is it possible to have multiple predicates in my XPath statement. For example, the...
7
by: Ot | last post by:
I posted this to the wrong group. It went to m.p.dotnet.languages.vb. Ooops. -------------------------------------------------------------------- I have this tiny problem. I have learned...
3
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml...
2
by: eggie5 | last post by:
I know how to edit my page using xpath selectors in firefox, it's just like this (in the else if). However, I can't find any way to do it in IE? I just want to find all links with the...
1
by: Alexey Titov | last post by:
Hi :) I need help selecting particular nodes with the SelectChildren method I get a simple xml from the web (excerpt): <ab:store> <ab:goods> <ab:icecream> <ab:flavor_01>
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.