Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Help with simple XPATH

Reply
Thread Tools

Help with simple XPATH

 
 
Monty
Guest
Posts: n/a
 
      12-30-2006
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"
xmlnssi="http://www.w3.org/2001/XMLSchema-instance">
<PROJECTNAME>
Project A
</PROJECTNAME>
<PROJECTBUDGET>
27000
</PROJECTBUDGET>
</PROJECTS>

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      12-30-2006
Monty wrote:

> The XPATH that does not work is /PROJECTS/PROJECTNAME.


> <PROJECTS xmlns="http://www.companyname.com/project"
> xmlnssi="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
/pfROJECTS/pfROJECTNAME

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/
 
Reply With Quote
 
 
 
 
Myron Turner
Guest
Posts: n/a
 
      01-05-2007
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"
> xmlnssi="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"
xmlnssl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml" >


<xsl:template xmlnsn="http://www.companyname.com/project" match="/">
<xsl:apply-templates select="pnROJECTS/pnROJECTNAME" />
<xsl:apply-templates select="pnROJECTS/pnROJECTBUDGET" />
</xsl:template>

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

<xsl:template xmlnsn="http://www.companyname.com/project"
match="pnROJECTBUDGET">
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/
 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Memory leak" in javax.xml.xpath.XPath Marvin_123456 Java 4 07-29-2005 03:49 PM
XPath: efficiency in xpath expressions Tjerk Wolterink XML 1 11-13-2004 06:03 PM
Are there any XPath parsers that generate XPath trees? goog XML 0 01-14-2004 01:47 PM
XPath that does not include other XPath Anna XML 0 07-31-2003 07:55 AM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM



Advertisments