Created
April 25, 2010 19:56
-
-
Save harrah/378669 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.xml._ | |
import transform._ | |
object Test { | |
// utility method for a simple interface to the transform package | |
def transform(xml: Node)(f: Node => Seq[Node]) = { | |
val rule = new RewriteRule { override def transform(n: Node) = f(n) } | |
val transformer = new RuleTransformer(rule) | |
transformer transform xml | |
} | |
// sample data | |
val xml = | |
<rules> | |
<rule>A</rule> | |
<rule>B</rule> | |
<rule> | |
<rule oRuleEdit="Areas">C</rule> | |
</rule> | |
<rule> | |
<rule>D</rule> | |
<rule>E</rule> | |
</rule> | |
</rules> | |
// some data to insert in the transformation | |
def newData = "3" | |
// transform the XML | |
val transformed = transform(xml) { | |
case n @ <rule>{c}</rule> if (n \ "@oRuleEdit").text == "Areas" => | |
<rule>{newData}</rule> | |
case n => n | |
} | |
println("\nRaw:\n\n" + xml) | |
println("\nTransformed:\n\n" + transformed ) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment