XPath and PHP

Discussion in 'XML & RSS' started by Omzy, Jun 2, 2009.

  1. #1
    How can I use XPath to read in an XML document, run a query to filter out some of the data and then save the filtered data as a new XML file?
     
    Omzy, Jun 2, 2009 IP
  2. mfurlong

    mfurlong Greenhorn

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #2
    If you are using a scripting language (e.g. PHP or ASP) or compiled language (e.g. VB.NET, C#.NET, etc.) that supports XML, then you could create an instance of an XML document programmatically. Then you could programatically filter, select, or transform your XML. The way in which this is done varies with the programming language and XML interface that the language supports.

    Another way is to use XSL which is a stylesheet language. First you create a an XSL stylesheet which can include XPATH for filtering or selection. Then you can put a statement in the XML file you want to process calling or referencing the XSL stylesheet. Some browsers (e.g. Internet Explorer) will process an XML file this way and call the referenced stylesheet before displaying the transformed output.

    >>> Mike
     
    mfurlong, Jun 3, 2009 IP
  3. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #3
    For that task I usually go with "xsltproc". It's a unix command to run a XSL file over an XML. Probably you can run xsltproc from PHP.
    Sometimes I write perl programs using the module XML::XPath ; is not very fast but work well. Surely there is a similar module for PHP.
    Both ways works only with XPATH 1.0, if you're trying to work with XPATH 2.0 you must try other ways.
     
    ajsa52, Jun 18, 2009 IP