-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unique identifier for the Node instances #403
Comments
Do you have a minimal example of the XML and calling the
I've never used it for XML, but Scala does have scala> n1 eq n2
res0: Boolean = false
scala> n1 eq n1
res1: Boolean = true However, |
Here an example: import cats.implicits._
import scala.util.{Failure, Success, Try}
import scala.xml.{NodeSeq, _}
import scala.xml.transform.{RewriteRule, RuleTransformer}
val doc = <persons><person name="David" /></persons>
case class Rule(zoom: NodeSeq => NodeSeq, action: NodeSeq => Try[NodeSeq]) { $rule =>
def toRewriteRule(wholeDocument: NodeSeq) : RewriteRule = new RewriteRule {
val target = $rule.zoom(wholeDocument)
val targetUpdated = $rule.action(target)
println("aa: " + target)
override def transform(ns: collection.Seq[Node]): collection.Seq[Node] =
if (ns.sameElements(target))
targetUpdated.get
else
ns
}
}
val rule : Rule = Rule(
zoom = _ \ "person" filter(_ \@ "name" == "David"),
ns => ns.map{
case e: Elem => Success(e.copy(child = e.child ++ <ToAppend/>))
case _ => Failure(new RuntimeException("Invalid type."))
}.toList
.sequence
.map(ns => NodeSeq.fromSeq(ns))
)
val rt: Seq[Node] = new RuleTransformer(rule.toRewriteRule(doc)).transform(doc) This works but if the input document was: <persons>
<person name="David" />
<node1>
<node2>
<person name="David" /> <!-- SAME AS FIRST -->
</node2>
</node1>
</persons> As result i've got: <persons>
<person name="David"><ToAppend/></person>
<node1>
<node2>
<person name="David"><ToAppend/></person>
</node2>
</node1>
</persons> This is right for the implementation i've done but i'm asking you if there is a way to resolve my problem |
There's still a lot happening in your example, but thanks for trying to minimize it. I've tried to simplify it further to show how to write the val doc =
<persons>
<person name="David" />
<skip>
<person name="David" />
</skip>
</persons>
val target: Node = (doc \ "person").filter(_ \@ "name" == "David").head
val update: Seq[Node] = target.map {
case elem: Elem => elem.copy(child = (elem.child ++ <toAppend/>))
case n => n
}
val rewriteRule = new RewriteRule {
override def transform(n: Node): Seq[Node] = {
if (target eq n)
update
else
n
}
}
val transform = new RuleTransformer(rewriteRule)
println(transform(doc)) |
Hi, i'm writing a Scala library to add a functional layer to this library.
In my library i've defined a rule to edit the xml document.
This rule contains:
With this rule instance i create a standard RewriteRule that will be applied to the whole document using a RuleTransformer.
The code is something like this:
Problem
The problem is: when the document has nodes with the same values
ns == target
returnstrue
multiple times, also if that node doesn't descends from the same parent.Question
There is a way to obtain an unique id for each node ? If not, it should be a valid proposal generating an unique id during the node instantiation and keep it as node field ?
The text was updated successfully, but these errors were encountered: