Help with simple XPATH

M

Monty

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>
 
M

Martin Honnen

Monty said:
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']
 
M

Myron Turner

Monty said:
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/
 

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,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top