phpxpath-users Mailing List for Php.XPath (Page 3)
Brought to you by:
bs_php,
nigelswinson
You can subscribe to this list here.
2001 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
(346) |
Nov
(8) |
Dec
(21) |
---|---|---|---|---|---|---|---|---|---|---|---|---|
2002 |
Jan
(30) |
Feb
(13) |
Mar
|
Apr
(3) |
May
(70) |
Jun
(26) |
Jul
(48) |
Aug
(22) |
Sep
(1) |
Oct
(4) |
Nov
|
Dec
|
2003 |
Jan
(3) |
Feb
(3) |
Mar
(11) |
Apr
(3) |
May
(4) |
Jun
(3) |
Jul
(2) |
Aug
(1) |
Sep
|
Oct
|
Nov
|
Dec
(2) |
2004 |
Jan
(4) |
Feb
(2) |
Mar
(2) |
Apr
(2) |
May
|
Jun
|
Jul
(12) |
Aug
(8) |
Sep
(2) |
Oct
(2) |
Nov
(3) |
Dec
|
2005 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
(1) |
Aug
(2) |
Sep
|
Oct
(1) |
Nov
(1) |
Dec
|
2006 |
Jan
(3) |
Feb
(7) |
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
2010 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
(1) |
Nov
|
Dec
|
From: Gabriel T. <gth...@we...> - 2004-01-21 17:54:45
|
Hello, I just can't figure out, how to get CDATA out of an element. My xml file: <?xml version="1.0" encoding="iso-8859-1"?> <Buyer> <User> <AboutMe>0</AboutMe> <AllowPaymentEdit>1</AllowPaymentEdit> <CIPBankAccountStored>0</CIPBankAccountStored> <CheckoutEnabled>1</CheckoutEnabled> <Email><![CDATA[gth...@we...]]></Email> <RegDate>2003-12-30 15:31:20</RegDate> <CountryCode>us</CountryCode> <Zip>94115</Zip> <UserId>mkgtest2</UserId> </User> </Buyer> This works fine: $user = $xpath->getData('/Buyer[1]/User[1]/UserId[1]/'); $user = $xpath->getData('/Buyer[1]/User[1]/UserId[1]/text()!!'); return is "mkgtest2". For the element "Email" i have not found anything that returns "gth...@we...". I tried: $user = $xpath->match('//Email'); It returns Array ( [0] => /Buyer[1]/User[1]/Email[1] ) Ok, that's fine, but when i try to use that xpath in $user = $xpath->getData('/Buyer[1]/User[1]/Email[1]'); or $user = $xpath->getData('/Buyer[1]/User[1]/Email[1]/text()!!'); i get nothing, not even an error. So what's the deal with the CDATA ? I guess i'm missing something because this can't be so hard. Thanks, Gabriel |
From: Peter R. <php...@pe...> - 2004-01-13 11:53:18
|
On Saturday 10 Jan 2004 17:43, darcy w.christ wrote: > i'm new to phpxpath. i've worked with the example a little and have > begun to get the hang of writing xpath statements. however, i'm still > having trouble understanding the best way to use this library. my > experience with xml is mostly with a dom parser, so in some ways, i'm > trying to understand the best way to use this code to do things like > loop through a series of nodes and process each of those nodes (and > their children) in specific ways. Xpath seems to be about referencing > the data in a node directly, rather than getting to a specific node > point and working with the children. i hope i'm making sense here. no, I'm not sure where your difficulty lies - xpath doesn't really care whether a node contains data or children. xml files are constructed in a hierarchy and you use xpath to access a particular point in the hierarchy. / is the top level; /node/node ... the lower levels. any given node may have child node(s), character data, or a mix of the two (+ entities, PIs etc, but we'll ignore them). phpxpath uses the sax parser to create a php array and uses the xpath hierarchy to access that array (php itself has xml commands to create arrays you can read and manipulate, but xpath is much easier to use). If you want a particular node and you know where in the hierarchy it is - /top[4]/middle[2]/bottom[13] - or whatever, you can access it directly with getData, appendChild or whatever. If you want to process child(ren) of a node you use match/evaluate; this returns an array of xpaths you can then iterate through as with accessing a particular node. So with the previous example //top[4]/middle[2] would return the xpath - node set - of all the children. Once you've found that the one you're looking for is bottom[13] you can access it directly with getData etc as above. any clearer? if not, try print_r on various evaluate statements on your xml file so you can see what they return. [ variations on this seem to be a FAQ, Nigel. Perhaps we need an example of evaluate in the distribution. ] |
From: darcy w.c. <da...@10...> - 2004-01-10 17:43:50
|
hi, i'm new to phpxpath. i've worked with the example a little and have begun to get the hang of writing xpath statements. however, i'm still having trouble understanding the best way to use this library. my experience with xml is mostly with a dom parser, so in some ways, i'm trying to understand the best way to use this code to do things like loop through a series of nodes and process each of those nodes (and their children) in specific ways. Xpath seems to be about referencing the data in a node directly, rather than getting to a specific node point and working with the children. i hope i'm making sense here. anyway, i'm asking the community if anyone could offer me some more sophisticated examples on how they use phpxpath to construct more robust code. i appreciate any help. ~darcy w. christ 1000camels |
From: Peter R. <php...@pe...> - 2003-12-08 11:42:13
|
On Sunday 07 Dec 2003 19:55, darcy w.christ wrote: > $xpath = new XPathEngine($xmlOptions); > > $xpath = $xpath->importFromString("<people><name > id=\"2\">fred</name></people>"); um, not tried this to see what it does, but Import returns true or false, so looks like you set $xpath to true :-) try leaving out "$xpath =" > // i know this is not right - so how to i use the XPath class > methods, once i have > // used XPathEngine? > $result = $xpath->getAttributes('//name[@id=2]','initial'); id is the attribute, 'fred' is the data content of the node. What is 'initial'? I would have thought there were 3 cases: either you have the name and want the id, in which case you use getAttributes for the node with the name you are looking for, or you have the id and want the name, in which case you use getData for the id, or you have neither and want a list, in which case you use match/evaluate and loop through the result. There's an example at the end of XPath.class.php |
From: darcy w.c. <da...@10...> - 2003-12-07 19:55:21
|
hi, i'm just beginning to work with PhpXPath. i'm having trouble understanding how to work with a string rather than a file. i've seen the method importFromString(), but i think my problem is that i don't understand how to apply the actual queries to it once it is loaded. Could someone give me an example or help me to understand how to work with both the XPathEngine and the XPath classes together. Below is what i have tried: $xmlOptions = array(XML_OPTION_CASE_FOLDING => FALSE, XML_OPTION_SKIP_WHITE => TRUE); $xpath = new XPathEngine($xmlOptions); $xpath = $xpath->importFromString("<people><name id=\"2\">fred</name></people>"); // i know this is not right - so how to i use the XPath class methods, once i have // used XPathEngine? $result = $xpath->getAttributes('//name[@id=2]','initial'); print_r($result); ~darcy w. christ 1000camels |
From: Nigel S. <nig...@us...> - 2003-08-10 20:27:23
|
Sorry this reply is so late... it's embarrasing. I read it hoping = someone else would reply cos I was busy, and then when I wasn't busy = your question had dropped off the radar :o( I am new to this class so please do not laugh if the question is = elementary. I am trying to user the following string as the query:=20 require_once("XPath.class.php");=20 $userID=3D'yyy';=20 $password=3D'xxx';=20 $url=3D"docs/userProfile.xml";=20 $xpath =3D new XPath($url);=20 $query =3D "/userProfiles/userProfile/general/userID=3D'$userID' AND = /userProfiles/userProfile/general/password=3D'$password'";=20 $tree =3D $xpath->exportAsXml($query);=20 and I am keep getting the error:=20 Notice: Undefined index: = /userProfiles/userProfile/general/userID=3D'yyy' AND = /userProfiles/userProfile/general/password=3D'xxx' in c:\program = files\apache group\apache\htdocs\plms\XPath.class.php on line 1235=20 Please help!=20 The exportAsXml() function takes as an arugment an "Absolute XPath". = This means that it must be the fully qualified address of a node in the = XML document. In the documentation introduction it describes this as: | AbsoluteXPath: (xPath, xPathSet) | A absolute XPath is a string. It 'points' to *one* node in the = XML-document. We use the | term 'absolute' to emphasise that it is not an xPath-query (see = xPathQuery). A valid xPath=20 | has the form like '/AAA[1]/BBB[2]/CCC[1]'. Usually functions that = require a node (see Node)=20 | will also accept an abs. XPath. So what you probably want to do instead is: require_once("XPath.class.php");=20 $userID=3D'yyy';=20 $password=3D'xxx';=20 $url=3D"docs/userProfile.xml";=20 $xpath =3D new XPath($url);=20 $query =3D "/userProfiles/userProfile/general/userID=3D'$userID' AND = /userProfiles/userProfile/general/password=3D'$password'";=20 $aMatches =3D $xpath->match($query); foreach($aMatches as $Match) { echo htmlspecialchars($xpath->exportAsXml($Match));=20 } Cheers Nigel |
From: Florent G. <fl...@mu...> - 2003-07-11 15:44:25
|
From: Behrouz H. <be...@ra...> - 2003-07-03 15:53:39
|
Hi, I am new to this class so please do not laugh if the question is elementary. I am trying to user the following string as the query: require_once("XPath.class.php"); $userID='yyy'; $password='xxx'; $url="docs/userProfile.xml"; $xpath = new XPath($url); $query = "/userProfiles/userProfile/general/userID='$userID' AND /userProfiles/userProfile/general/password='$password'"; $tree = $xpath->exportAsXml($query); and I am keep getting the error: Notice: Undefined index: /userProfiles/userProfile/general/userID='yyy' AND /userProfiles/userProfile/general/password='xxx' in c:\program files\apache group\apache\htdocs\plms\XPath.class.php on line 1235 Please help! Behrouz Heshmatipour E-mail: be...@ra... Office Phone: 1 (866) RAYANE-1 |
From: Peter R. <php...@pe...> - 2003-06-21 08:39:23
|
On Saturday 21 Jun 2003 08:33, Peter James wrote: > and I do a //img. This will give me all img elements in the doc. > If I do //img[1] I still get the first element in all paths. If I > do (//img)[1], I get all img elements, where I should just get the > first element in the document. see note at http://www.w3.org/TR/xpath#path-abbrev "NOTE: The location path //para[1] does not mean the same as the location path /descendant::para[1]. The latter selects the first descendant para element; the former selects all descendant para elements that are the first para children of their parents." |
From: Peter J. <pe...@in...> - 2003-06-21 07:32:45
|
Hello all. Imagine I have an XHTML doc like so: <html> < ... > <body> <img src= ...> <table> <tr> <td><img src= .. > ... and I do a //img. This will give me all img elements in the doc. If I do //img[1] I still get the first element in all paths. If I do (//img)[1], I get all img elements, where I should just get the first element in the document. I looked at the source, and it seems like it strips off the brackets and ignores the predicate. Is that correct? Do you see support for this anytime soon? (I know I can just pull the first one off of the array I get back, but that's not xpath :-) Just curious. Thanks, Pete. -- Peter James pe...@ph... php|architect The Magazine for PHP Professionals http://www.phparch.com |
From: Nigel S. <nig...@us...> - 2003-06-01 00:52:34
|
Release notes: https://sourceforge.net/project/shownotes.php?group_id=36731&release_id=162796 Project home page: http://sourceforge.net/projects/phpxpath A bug fix release with a couple of new XPath functions permitting case insensitive matching, and generate-id() for creating table of contents. As always, any bug reports are gratefully appreciated, and if you don't report it, then there's a very high chance that it'll never get fixed :o) Cheers all, Nigel =========================== For the most recent version of Php.XPath, and an archive of this list visit: http://www.sourceforge.net/projects/phpxpath |
From: Peter R. <php...@pe...> - 2003-05-14 08:55:56
|
On Tuesday 13 May 2003 23:39, Magno Cardona wrote: > I have a xml title/content. > <cdi> > <title> > <content> > <title id="WATERCOOLING"> > <content/> > </title> > </content> > </title> > <cdi> > > Now I want to return the xml of a title that matches the XPATH > //title[@id='WATERCOOLING']. > > My problem usint this class is that I cant get the abs path so I > can use the > <file:///C:\Documents%20and%20Settings\Magno\Desktop\test\Php.XPath >Documenta tion.html#exportAsXml#exportAsXml> exportAsXml > > Well I hope I can receive help > > PD: Remember the title can be 20 level inside so writing manually > the abs. path is absurd. not sure I understand the problem, Magno. You can use match/evaluate to fetch the relevant path(s) and then your export as appropriate, e.g.: $nodes = $xml->evaluate("//title[@id='WATERCOOLING']"); foreach ($nodes as $node) print $xml->exportAsXml($node); |
From: Magno C. <ma...@en...> - 2003-05-13 22:39:39
|
Hello, =20 I have a xml title/content. <cdi> <title> <content> <title id=3D"WATERCOOLING"> <content/> </title> </content> </title> <cdi> =20 Now I want to return the xml of a title that matches the XPATH //title[@id=3D'WATERCOOLING']. =20 My problem usint this class is that I cant get the abs path so I can use = the <file:///C:\Documents%20and%20Settings\Magno\Desktop\test\Php.XPathDocume= nta tion.html#exportAsXml#exportAsXml> exportAsXml=20 =20 Well I hope I can receive help =20 PD: Remember the title can be 20 level inside so writing manually the = abs. path is absurd. =20 |
From: Peter R. <php...@pe...> - 2003-05-09 09:51:11
|
On Thursday 08 May 2003 08:07, Hrotk=F3 G=E1bor wrote: > Hi! > > When i have this xml: > <?xml version=3D"1.0" encoding=3D"UTF-8"?> > <root> > <node> > <tag attrib=3D"1/1">item1/1</tag> > <tag attrib=3D"1/2">item1/2</tag> > <tag attrib=3D"1/3">item1/3</tag> > </node> > <node> > <tag attrib=3D"2/1">item2/1</tag> > <tag attrib=3D"2/2">item2/2</tag> > <tag attrib=3D"2/3">item2/3</tag> > </node> > </root> > > I can use the @ to search for nodes, that has the specified > attribute, like: > > match("/root/node/tag[@attrib=3D'1/2']") > would result: > /root[1]/node[1]/tag[2] > > But how can i search for a node with specified text data, for > example to have the result > /root[1]/node[2]/tag[3] > when i search for the text data: "item2/3"? > For example match("/root/node/tag=3D'item2/3'") or something would be > fine, but now, it isn't working for me. either //*[tag=3D'item2/3'] meaning fetch all nodes with a child element called tag whose content=20 =3D item2/3 or //node[tag=3D'item2/3'] meaning: fetch all nodes called node with a child element called tag=20 whose content =3D item2/3 Check out phpxpath's testbench (testBench/useCases/index.php at=20 wherever you've installed the software) for working out xpath syntax.=20 The syntax is quite simple once you get the hang of it, but it is=20 confusing at first and the official w3c documentation doesn't contain=20 nearly enough examples. |
From:
<ro...@pr...> - 2003-05-08 07:08:11
|
Hi! When i have this xml: <?xml version="1.0" encoding="UTF-8"?> <root> <node> <tag attrib="1/1">item1/1</tag> <tag attrib="1/2">item1/2</tag> <tag attrib="1/3">item1/3</tag> </node> <node> <tag attrib="2/1">item2/1</tag> <tag attrib="2/2">item2/2</tag> <tag attrib="2/3">item2/3</tag> </node> </root> I can use the @ to search for nodes, that has the specified attribute, like: match("/root/node/tag[@attrib='1/2']") would result: /root[1]/node[1]/tag[2] But how can i search for a node with specified text data, for example to have the result /root[1]/node[2]/tag[3] when i search for the text data: "item2/3"? For example match("/root/node/tag='item2/3'") or something would be fine, but now, it isn't working for me. Thanx Roti |
From: Nigel S. <nig...@us...> - 2003-04-24 20:20:27
|
Hi Paul, getData() takes: string $xPathQuery xpath to the node - resolves to *one* xpath. So the error message is saying that "//directory[name=help]/logo" matches more than one node (or no node). So it doesn't know which node to return you the data of. Try: $aMatches = $xpath->match("//directory[name='help']/logo"); print_r($aMatches); $logo= $xpath->getData($aMatches[0]); echo $logo; (Note the ' and the call to match before the call to getData). You might get away with: $logo= $xpath->getData("//directory[name='help']/logo"); echo $logo; Cheers Nigel ----- Original Message ----- From: "Paul Arnold" <Pa...@pi...> To: <nig...@us...> Sent: Thursday, April 24, 2003 4:31 PM Subject: php.XPath class question Hi Nigel, I wonder if you can help me at all. If I try to carry out $logo= $xpath->getData("//directory[name=help]/logo"); echo $logo; or $logo= $xpath->getData("//directory[@name=help]/logo"); echo $logo; on the piece of xml below I get the following error: XPath error in XPath.class.php:5061 The supplied xPath '' does not *uniquely* describe a node in the xml document. Is this correct? Is it a bug? Can you tell me how I select the //directory[@name=help]/logo node? <root> <directory name="company"> <logo>images/logo.gif</logo> </directory> <directory name="help"> <logo>images/logohelp.gif</logo> </directory> </root> Kind regards, Paul Arnold Software Engineer Pivetal Ltd. Tel: 023 8021 5302 Email: pa...@pi... |
From: Nigel S. <nig...@us...> - 2003-04-09 22:59:50
|
First of all, sorry about my last post (php = allow_call_time_pass_reference 03/23/2003). At the time, I don't know = why, but I couldn't find any info. I now have read the posts previosu to = mine and understand the issue. If you are an administrator and can = delete that post, please do (along with this part of this message if = possible). The issue was always somewhat fuzzy in my mind, so I was hoping someone = else would chip in and answer your question.... the email was still in = my inbox :o/ Glad you got the answer you were looking for though! I have another issue now that I can't seem to find any info on. Using = phpXpath 3.3 I am able to import xml using both the importFromFile() and = importFromString() methods. I am also able to use the getData() and = exportAsXml() methods. However my application calls for using = appendChild() / insertChild() and insertData() calls inbetween. For some = reason, my appendChild() calls are returning the following errors: <snip> My appendChild() call looks like this: $XpathObject->appendChild("/MyRootNode[1]/SubNodeOne[1]", = "SubNodeTwo"); Try=20 $XpathObject->appendChild("/MyRootNode[1]/SubNodeOne[1]", = "<SubNodeTwo/>"); Is the error coming from a problem with my method call, or is it from = the fact that the zero element of this array is empty / null? How can I = fix this? I tried substituting the version 2.2 class source file instead = of version 3.3, and everything works fine. After viewing my xml files = using several text editors, I've come to the conclusion that the error = is not because of whitespace at the beginning of the document. I get = this error from both importFromString() and importFromFile(), since the = latter actually calls the first. Can anyone shed any light on why this = is happening? Thanks! It was a change in the appendChild() call between 2.2 and 3.3 that we = obviously didn't document well enough (you aren't the first to get stung = by this). Previously you could only append a single new element, but = now you can do cool stuff like=20 $XpathObject->appendChild("/MyRootNode[1]/SubNodeOne[1]", = "<a><b/><c/></a>"); Cheers Nigel |
From: Dan <xp...@da...> - 2003-04-08 17:59:34
|
First of all, sorry about my last post (php allow_call_time_pass_reference 03/23/2003). At the time, I don't know why, but I couldn't find any info. I now have read the posts previosu to mine and understand the issue. If you are an administrator and can delete that post, please do (along with this part of this message if possible). I have another issue now that I can't seem to find any info on. Using phpXpath 3.3 I am able to import xml using both the importFromFile() and importFromString() methods. I am also able to use the getData() and exportAsXml() methods. However my application calls for using appendChild() / insertChild() and insertData() calls inbetween. For some reason, my appendChild() calls are returning the following errors: XPath error in oop.xpath.php:1673 In importFromString(): XML error in given string on line 1 column 0. Reason:syntax error XPath error in oop.xpath.php:5272 XPath error in oop.xpath.php:1673. Message: In importFromString(): XML error in given string on line 1 column 0. Reason:syntax error When I comment out the appendChild() call, both errors disappear. I tried setting $bDebugThisFunction of importFromFile() to true, and got the following (abbreviated) output: Setup for parse 1 ms Parse Object 4 msArray ( [0] => [1] => /text()[1] [2] => /MyRootNode[1] [4] => /SubNodeOne[1]/text()[1] ... [22] => /SubNodeOne[1]/text()[3] [23] => /text()[2] ) My appendChild() call looks like this: $XpathObject->appendChild("/MyRootNode[1]/SubNodeOne[1]", "SubNodeTwo"); Is the error coming from a problem with my method call, or is it from the fact that the zero element of this array is empty / null? How can I fix this? I tried substituting the version 2.2 class source file instead of version 3.3, and everything works fine. After viewing my xml files using several text editors, I've come to the conclusion that the error is not because of whitespace at the beginning of the document. I get this error from both importFromString() and importFromFile(), since the latter actually calls the first. Can anyone shed any light on why this is happening? Thanks! Dan xp...@da... |
From: Dan <xp...@da...> - 2003-03-24 04:51:15
|
I started using this object back when it was called phpxml -- very useful for assembling UPS & USPS xml requests and easily deciphering xml responses from either shipping tool. I thought it might be a good idea to upgrade my code to make use of the new Php.XPath 3.3 object because the old phpxml object threw the following error: Call-time pass-by-reference has been deprecated - argument passed by value. If you would like to pass it by reference, modify the declaration of {function_name()}. If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file. However, future versions may not support this any longer. So, I modified the INI file and all was good. However, I'm concerned about using this option because it has been deprecated and may not be supported in future releases of php. I've noticed that Php.XPath throws the same error with allow_call_time_pass_reference set to false, in 3 places in the XPathEngine class. Can the XPathEngine class be modified so as to not pass arguments by reference? Will this cause problems with the object? Is there a particular reason the developers of this object employ reference calls that have been deprecated? For example, can... xml_set_object($parser, &$this); ... be replaced with... xml_set_object($parser, $this); ... and still function properly? Just wanted an expert answer before I go and update all the shopping carts I host with the new code. Thank you for your time and help! Dan xp...@da... |
From: Peter R. <php...@pe...> - 2003-03-17 12:37:51
|
On Monday 17 Mar 2003 11:38, Eric Anderson wrote: > On Mon, 2003-03-17 at 06:15, Peter Robins wrote: > > replace 'find' with 'evaluate' (or 'match') and 'getText' with > > 'getData' and that should do what you're looking for (though you > > might want to change your location path to e.g. //tag to fetch > > all the descendant nodes) > > But according to the documentation and my experience with the > library, 'evaluate' and 'match' will only resolve a XPathQuery if > it resolves to one node. If it resolves to a collection of nodes > (or no nodes), then an error is generated. not on the copy I have! from the documentation for match: "Matches (evaluates) an XPath query . . . Return Value: The result of the XPath expression. Either: node-set (an ordered collection of nodes without duplicates) . . ." Though it doesn't actually say so, what it returns is an array of paths, which you can then use in your for loop (or whatever) to getData (or whatever). So in your example //tag will return /root[1]/tag[1] /root[1]/tag[2] /root[1]/tag[3] |
From: Eric A. <eri...@co...> - 2003-03-17 11:28:35
|
On Mon, 2003-03-17 at 06:15, Peter Robins wrote: > replace 'find' with 'evaluate' (or 'match') and 'getText' with > 'getData' and that should do what you're looking for (though you > might want to change your location path to e.g. //tag to fetch all > the descendant nodes) But according to the documentation and my experience with the library, 'evaluate' and 'match' will only resolve a XPathQuery if it resolves to one node. If it resolves to a collection of nodes (or no nodes), then an error is generated. -- Eric Anderson There are 10 types of people in the word Those who can read binary, and those who can't |
From: Peter R. <php...@pe...> - 2003-03-17 11:15:28
|
On Sunday 16 Mar 2003 02:33, Eric Anderson wrote: > I am having > trouble figuring out how to do a XPath query and get back a list of > nodes. So for example if I have a XML file as follows: > > <root> > <tag>item1</tag> > <tag>item2</tag> > <tag>item3</tag> > </root> > > I want to be able to say something like: > > $xPath = new XPath(); > $xPath->importFromFile( $filename ); > $list = $xPath->find( '/root/tag' ); > foreach ($list as $item) { > echo $item->getText() . '<br>'; > } replace 'find' with 'evaluate' (or 'match') and 'getText' with 'getData' and that should do what you're looking for (though you might want to change your location path to e.g. //tag to fetch all the descendant nodes) |
From: Eric A. <eri...@co...> - 2003-03-16 02:24:54
|
All my XPath hacking has previously been with Perl using XML::XPath, but I am working on a PHP project and I like using XPath retrieve config info in little XML files. I came across this project and thought that it looked pretty good, but I am having trouble figuring out how to do a XPath query and get back a list of nodes. So for example if I have a XML file as follows: <root> <tag>item1</tag> <tag>item2</tag> <tag>item3</tag> </root> I want to be able to say something like: $xPath = new XPath(); $xPath->importFromFile( $filename ); $list = $xPath->find( '/root/tag' ); foreach ($list as $item) { echo $item->getText() . '<br>'; } and have it output: item1 item2 item3 This is somewhat like the Perl syntax (mixed with a bit of your libraries). But things that are missing are a method that takes a XPathQuery and returns a list of nodes that match that query, and a method on the node that will return the text inside the node. I could probably achieve the second needed method by using the join function on 'textParts' attribute of the Node, but that seems a bit messy. Basically I am wondering what is the best way of reading a list of nodes that are returned from a XPathQuery. Everything in PHP.XPath seems to be based around giving an absolute XPathQuery or a requiring that only one node being returned by a query. How do you get a list of nodes that match a path? Thanks for your help and the great library. XPath greatly simplifies my code, and I will be real happy if I can use it in this PHP project. -- Eric Anderson There are 10 types of people in the word Those who can read binary, and those who can't |
From: Nigel S. <nig...@us...> - 2003-03-11 02:28:13
|
Thank you for your kind reply, now I can find the data I want.=20 But, I still couldn't find a way to count how many books are in the = library? count($xPath->match(/LIBRARY[1]/BOOK)) And I want to import the all book data(title, author,pages,price etc.) = into a php array and present them to the user in other format.(For = example a HTML form select menu options) Is my idea workable with Xpath? Yes very much so :o) Something along the lines of: $aBookAddress =3D $xPath->match('/LIBRARY[1]/BOOK'); foreach($aBookAddress as $BookAddress) { $aBook =3D array(); $aBook['Title'] =3D $xPath->getData($BookAddress.'/title'); $iISBN =3D $xPath->getAttribute($BookAddress.'/title', 'ISBN'); $aBooks[$iISBN] =3D $aBook; } (The above is completely untested, written off the top of my head, but = it's a good template to start from) Cheers Nigel |
From: Ji T. <ji...@to...> - 2003-03-09 08:15:20
|
SGkhIA0KVGhhbmsgeW91IGZvciB5b3VyIGtpbmQgcmVwbHksIG5vdyBJIGNhbiBmaW5kIHRoZSBk YXRhIEkgd2FudC4gDQoNCkJ1dCwgSSBzdGlsbCBjb3VsZG4ndCBmaW5kIGEgd2F5IHRvIGNvdW50 IGhvdyBtYW55IGJvb2tzIGFyZSBpbiB0aGUgbGlicmFyeT8NCkFuZCBJIHdhbnQgdG8gaW1wb3J0 IHRoZSBhbGwgYm9vayBkYXRhKHRpdGxlLCBhdXRob3IscGFnZXMscHJpY2UgZXRjLikgaW50byBh IHBocCBhcnJheSBhbmQgcHJlc2VudCB0aGVtIHRvIHRoZSB1c2VyIGluIG90aGVyIGZvcm1hdC4o Rm9yIGV4YW1wbGUgYSBIVE1MIGZvcm0gc2VsZWN0IG1lbnUgb3B0aW9ucykNCg0KSXMgbXkgaWRl YSB3b3JrYWJsZSB3aXRoIFhwYXRoPw0KDQpBZ2FpbixhdHRhaGNlZCBteSBzYW1wbGUgeG1sIGZp bGUuDQo9PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09 PT09PT09PT09PT09PT09PT09DQo8P3htbCB2ZXJzaW9uPSIxLjAiPz4NCjxsaWJyYXJ5Pg0KIDxi b29rIGlzYm49IjA0NDAyMjQ2NzUiPg0KICA8dGl0bGU+SGFubmliYWw8L3RpdGxlPg0KICA8YXV0 aG9yPlRob21hcyBIYXJyaXM8L2F1dGhvcj4NCiAgPGdlbnJlPlN1c3BlbnNlPC9nZW5yZT4NCiAg PHBhZ2VzPjU2NDwvcGFnZXM+DQogIDxwcmljZT44Ljk5PC9wcmljZT4NCiAgPHJhdGluZz40PC9y YXRpbmc+DQogPC9ib29rPg0KIDxib29rIGlzYm49IjA0NTE0MDk4MDkiPg0KICA8dGl0bGU+UnVu PC90aXRsZT4NCiAgPGF1dGhvcj5Eb3VnbGFzIEUuIFdpbnRlcjwvYXV0aG9yPg0KICA8Z2VucmU+ VGhyaWxsZXI8L2dlbnJlPg0KICA8cGFnZXM+MzkwPC9wYWdlcz4NCiAgPHByaWNlPjcuNDk8L3By aWNlPg0KICA8cmF0aW5nPjU8L3JhdGluZz4NCiA8L2Jvb2s+DQo8L2xpYnJhcnk+DQo9PT09PT09 PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09 PT09PT09PQ0KDQpCZXN0IHdpc2hlcywNCkppIFRhbw0KICAtLS0tLSBPcmlnaW5hbCBNZXNzYWdl IC0tLS0tIA0KICBGcm9tOiBOaWdlbCBTd2luc29uIA0KICBUbzogSmkgVGFvIA0KICBDYzogcGhw eHBhdGgtdXNlcnNAbGlzdHMuc291cmNlZm9yZ2UubmV0IA0KICBTZW50OiBTdW5kYXksIE1hcmNo IDA5LCAyMDAzIDU6NDEgQU0NCiAgU3ViamVjdDogUmU6IE5ldyB1c2UgbmVlZCB5b3VyIGhlbHAN Cg0KDQogIEJlY2F1c2UgeW91IGFyZSB1c2luZyB0aGUgWE1MX09QVElPTl9DQVNFX0ZPTERJTkcg b3B0aW9uLCB5b3UgbmVlZCB0byBkbzoNCg0KICAkZmRhdGEgPSAmJHhQYXRoLT5nZXROb2RlKCcv TElCUkFSWVsxXS9CT09LWzFdJyk7DQoNCiAgU29ycnkgZm9yIHRoZSBjb25mdXNpb24uDQoNCiAg Q2hlZXJzDQoNCiAgTmlnZWwNCg0KICAgIC0tLS0tIE9yaWdpbmFsIE1lc3NhZ2UgLS0tLS0gDQog ICAgRnJvbTogSmkgVGFvIA0KICAgIFRvOiBOaWdlbCBTd2luc29uIA0KICAgIENjOiBwaHB4cGF0 aC11c2Vyc0BsaXN0cy5zb3VyY2Vmb3JnZS5uZXQgDQogICAgU2VudDogU2F0dXJkYXksIE1hcmNo IDA4LCAyMDAzIDU6MzUgUE0NCiAgICBTdWJqZWN0OiBSZTogTmV3IHVzZSBuZWVkIHlvdXIgaGVs cA0KDQoNCiAgICBIaSBOaWdlbCwNCiAgICBUaGFua3MgZm9yIHlvdXIgcmVwbHkuIEkgdHJpZWQg d2hhdCB5b3Ugc2FpZCAnL2xpYnJhcnlbMV0vYm9va1sxXScsIGJ1dCBpdCBzdGlsbCByZXR1cm4g bm90aGluZy4NCiAgICBDYW4geW91IHRyeSB0byBydW4gdGhpcyBzbWFsbCBzYW1wbGU/DQogICAg SXQgbXVzdCBiZSBzb21ldGhpbmcgZWxzZS4gYnV0IEkgZG9uJ3Qga25vdy4NCg== |