Skip to content
Use QueryPath to run replacement tasks inside of Phing build.xml files
PHP
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
src/PhingQueryPath/Task
tests
.gitignore
COPYING-MIT.txt
README.md
build.xml

README.md

QueryPath Tasks for Phing

This package provides Phing tasks that let you use QueryPath to process XML or HTML.

In a nutshell, it allows you to define rules composed of two parts:

  • A CSS 3 selector (a la jQuery and QueryPath)
  • A replacement value

The qpreplace task will then seek through all of the files in a file set, looking for each selector. Every time the selector finds a match, the replacement value is substituted.

Example

Imagine starting with an XML file named foo.xml that looks like this:

<?xml version="1.0">
<foo>
  <bar>REPLACE ME</bar>
</foo>

Using the QueryPath replacement task, you could then define a target that does this:

<target name="replace">
  <qpreplace>
    <fileset dir="path/to/foo.xml">
      <include name="foo.xml"/>
    </fileset>
    <rule selector="bar">Replacement</rule>  
  </qpreplace>
</target>

The above will scan foo.xml and replace the contents of the bar element with the text Replacement. Thus, after running this task, your foo.xml would look like this:

<?xml version="1.0">
<foo>
  <bar>Replacement</bar>
</foo>

You can build much more sophisticated tools, though, scanning entire sets of files and making multiple substitutions on each. Your selectors can be much more sophisticated, too (#particularID foo>bar.myClass:not(myOtherClass):first)

(See the Usage section below for info on how to configure your environment to use qpreplace)

Why/when is this useful?

In general, it is useful when you have HTML or XML files that need automatic replacements that are sophisticated enough that mere token replacement (a built-in feature of Phing) is not sufficient.

Possible examples:

  • Post-processing the package.xml file generated by Phing's Pear Package tasks
  • Processing generated HTML, such as PhpDocumentor or PHPUnit output
  • Manipulating your own build.xml file from within build.xml

Installation

  • QueryPath must be installed before this will work.
  • Phing must be installed for this to work.

With PEAR:

$ pear channel-discover pear.querypath.org
$ pear install querypath/QueryPath
$ pear install querypath/PhingQueryPath

From Source

  1. Get the source
    • You can clone git
    • You can download from GitHub
  2. Put the code in a path accessible to Phing

Usage

To use:

Edit your build.xml file to map the QueryPathReplacementTask.php to a task element:

<!-- You might need some variant of this if Phing cannot find QueryPath. -->
<!-- includepath classpath="path/to/QueryPath"/ -->

<!-- You might need some variant of this if Phing cannot find the task. -->
<!-- includepath classpath="path/to/Phing-QueryPath"/ -->

<!-- Task definition for QueryPathReplacementTask. -->
<taskdef classname="PhingQueryPath.Task.QueryPathReplacementTask" name="qpreplace"/>

Now, somewhere else in your build.xml, you can use qpreplace to run replacements:

<qpreplace>
  <fileset dir="path/to/xml/files">
    <include name="*.xml"/>
  </fileset>
  <rule selector="targetOne">FOO</rule>
  <rule selector="div>targetTwo">FOO2</rule>
  <rule selector="#three>targetThree">FOO3</rule>
</qpreplace>

The above reads in all of the XML files as a fileset, and then applies the rule replacements to each file. Note that the selector attributes are CSS 3 selectors (just like jQuery and QueryPath use), and the text value is what will replace the selector. Of course you can use Phing variables in either case. In other words, this would be a valid rule:

<property name="selector" value="#content > div.lede:first"/>
<property name="replacement" value="can haz cheezburger?"/>
<qpreplace>
  <fileset dir="path/to/xml/files">
    <include name="*.xml"/>
  </fileset>
  <rule selector="${selector}">${replacement}</rule>
</qpreplace>

That's all there is to using this task. It can be especially useful for post-processing XML file that are generated automatically during a phing build (for example PearPackage2's package.xml file).

Something went wrong with that request. Please try again.