473,624 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP5 Xpath problem

Hi all,

I have recently started working with the new XML functionality in
PHP5, but I am running into a few problems. Specifically, I am using
an Xpath query to try and pull out the data in specific elements
within my XML file. The problem I am getting is that one of the XML
elements contains namespace attributes and this seems to prevent the
Xpath query from working (ie, if I manually strip the namespace
attributes then the query works fine. After testing I will not have
direct access to the XML file, so stripping the namespace attributes
each time is not an option.

Can anyone shed any light as to why this might be so, or offer any
suggestions to overcome the problem?

tia

Chris

############### ############### #########
Example code:

<?
$dom = new DomDocument();
$dom->load("ltsnec.x ml");
$path =
"/OAI-PMH/ListRecords/record/metadata/lom/general/title/string";

// XPath query
$xp = new DomXpath($dom);
$titles = $xp->query($path) ;

$length = $titles->length;

// this echoes 0 when the namespace attributes are present
echo "<p>Number = " . $length . "</p>";

foreach ($titles as $node) {
echo "<p>:: " . $node->textContent . "</p>";
}
?>

Example XML which works

<OAI-PMH>
<ListRecords>
<record>
<header>

<identifier>htt p://www.english.lts n.ac.uk/repository/lom/esc_100.xml</identifier>
<datestamp>20 04-7-22</datestamp>
</header>
<metadata>
<lom>
<general>
<identifier>htt p://purl.org/poi/english.ltsn.ac .uk/100</identifier>
<title>
<string language="en-GB">Universit y of Leeds - Bodington Common
Sites</string>
</title>
<language>en-GB</language>
<description>
<string language="en-GB">Blah blah blah</string>
</description>
</general>

....etc...

</lom></metadata></record></ListRecords></OAI-PMH>

Example XML which doesn't work

<OAI-PMH>
<ListRecords>
<record>
<header>

<identifier>htt p://www.english.lts n.ac.uk/repository/lom/esc_100.xml</identifier>
<datestamp>20 04-7-22</datestamp>
</header>
<metadata>

## problem occurs when the lom tag includes the namespace attributes
## as follows

<lom xmlns="http://ltsc.ieee.org/xsd/LOMv1p0"
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocat ion="http://ltsc.ieee.org/xsd/LOMv1p0
http://www.rdn.ac.uk/oai/lom/lom.xsd">
<general>
<identifier>htt p://purl.org/poi/english.ltsn.ac .uk/100</identifier>
<title>
<string language="en-GB">Universit y of Leeds - Bodington Common
Sites</string>
</title>
<language>en-GB</language>
<description>
<string language="en-GB">Blah blah blah</string>
</description>
</general>

....etc...

</lom></metadata></record></ListRecords></OAI-PMH>
Jul 17 '05 #1
1 2367
On Wed, 18 Aug 2004 10:08:52 +0100, Chris wrote:
I have recently started working with the new XML functionality in
PHP5, but I am running into a few problems. Specifically, I am using
an Xpath query to try and pull out the data in specific elements
within my XML file. The problem I am getting is that one of the XML
elements contains namespace attributes and this seems to prevent the
Xpath query from working (ie, if I manually strip the namespace
attributes then the query works fine. After testing I will not have
direct access to the XML file, so stripping the namespace attributes
each time is not an option.

Can anyone shed any light as to why this might be so, or offer any
suggestions to overcome the problem?


I can't claim to have had huge success with XPath in PHP5 when dealing
with XML Namespaces myself, but you can try calling the registerNamespa ce
method of the $xp object with the namespace details before running any
queries, provided you know what the namespaces are ahead of time (or don't
mind looking the information up with something like getElementsByTa gNameNS.

It's one of those things which at least seems like the right thing to do;
hopefully we get a bit more documentation on it at some point.

--
Adam Harvey
Optimiser Pty Ltd

To e-mail: don't make an example out of me!

Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2469
by: Philipp Lenssen | last post by:
I noticed there's an error in PHP when it receives form data. It seems to precede various characters with "\". I was wondering if that's about to get fixed in PHP5, or already is fixed in PHP4? (Or is this somehow not an error?) Also, I was wondering when/ if PHP will include more object-oriented functions, like $myString.replace($find, $replaceWith). (I sometimes wonder if I need to use the first or second or third parameter or what...
1
1809
by: Philipp Lenssen | last post by:
I'm looking for a way to grab *all* XML of a DOM node (in PHP5). Something like the following, only it doesn't work! Thanks. /* I want everything (all XHTML) in <div id="hell=">...</div> */ $dom = new domdocument; @$dom->loadHTMLFile('http://www.example.com'); $xpath = new domxpath($dom); $xNodes = $xpath->query("//div//p/*"); $sText = '';
2
1432
by: Börni | last post by:
Hi, I couldnt find an answer to my question in the php manual, so i try it here. Basically i just want to know if the following is possible. Suppose you have this xml file: <root> <news id="1"> <title>Test</title>
3
3005
by: Philipp Lenssen | last post by:
I want to try the new Yahoo API, but I get the PHP5 error message Unknown encoding "utf-8" in the marked line below... (After that, applying the Xpath will fail as symptom of the first bug.) function showImages($q) {
4
21201
by: Son KwonNam | last post by:
In XSLT, is this possible to get value from xml using XPath which is in XSLT variable? I mean XPath strings can be dynamic while XSL Transforming. If possible, How?? Because I'm not a native English speaker, it's quite hard to make the problem clear. Please see the following example.
4
2017
by: Skeets | last post by:
When the data structure looks like this: <page> <point id="1"> <premise> 1+1=2 </premise> </point> <point id="2"> <premise> round is better than square </premise> </point> <thought id="1">
2
2582
by: Stefan Huber | last post by:
Hi I've got a really strange problem, and can't find out why it's not working as intended. in order to use php4 and 5 together on a webserver and the requirement for running as different users, I use suexec and a wrapper script for php files. to make it a bit clearer, i'll post the different snippets: httpd.conf:
2
2752
by: Weston | last post by:
Poking around with XPath using SimpleXML, it looks like there are at least a few reasonably common XPath operators and predicates that aren't supported. I'd like to check my observations against other people's experience, and find out if I'm missing something. Here's what it looks like to me in PHP 5.2.0: * the count() predicate seems to fail quietly (no error or warning, you just receive a false value from the xpath method rather than...
1
2226
by: juicymixx | last post by:
I must be completely missing something here. I can't seem to figure out how to parse using the DOM in PHP5... For instance, as a test I'm trying to scrape out the weather conditions table from: http://www.ufl.edu/weather I've tried several options, but am currently working on: $url = 'http://www.ufl.edu/weather'; $dom = new DOMDocument(); $doc->validateOnParse = true; $dom->loadHTML($url);
0
8675
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8619
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
8334
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
8474
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5561
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
4078
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
4173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.