Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 30th, 2006, 12:15 PM
Monty
Guest
 
Posts: n/a
Default 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>

  #2  
Old December 30th, 2006, 01:05 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: Help with simple XPATH

Monty wrote:
Quote:
The XPATH that does not work is /PROJECTS/PROJECTNAME.
Quote:
<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/
  #3  
Old January 5th, 2007, 05:35 AM
Myron Turner
Guest
 
Posts: n/a
Default Re: Help with simple XPATH

Monty wrote:
Quote:
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/
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles