Re: general guidance
Brought to you by:
bs_php,
nigelswinson
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. ] |