Scala xml RewriteRules似乎不按顺序应用

时间:2016-09-20 19:38:49

标签: xml scala

我试图将包含一堆RewriteRules的对象放在一起,并且可以返回RuleTransformer。这是有效的,但由于某种原因,最后两个RewriteRules(以及其他我认识的人)似乎都是乱序应用的。

这是一套简化的规则,包括我返回的RuleTransformer:

  def getTransformer(): RuleTransformer = {
    val contentSourceNodeLabels = List("contentgroup", "byline")
    val copyrightReplacementMapping = Map(
      """Version 1""".r  -> "All rights reserved. Used with permission.",
      """Version 2""".r    -> "By example.com, © Acme Co. All rights reserved. Used with permission.",
      "".r                -> "By example.com, © Acme Co. All rights reserved. Used with permission."
    )
    val metadataNodeOrder = List(
      "title",
      "subtitle",
      "abstract",
      "copyrighttext"
    )

    new RuleTransformer(
      new alterCopyrightNode("contentitem", contentSourceNodeLabels, copyrightReplacementMapping),
      new alterMetadataOrder(metadataNodeOrder)
    )
  }

请注意,metadataNodeOrder中的最后一个标签是" copyrighttext"。为了正确排序元数据元素,alterMetadataOrder需要更改版权元素标签" copyright"到" copyrighttext" - 这是alterCopyrightNode规则的作用。所以这些规则的顺序很重要。

我为每个规则添加了println(),事实上,它们似乎确实无序运行:

> run data/articles_20120711.xml

data/articles_20120711.xml:
  ..formatted
  >> [alterMetadataOrder]: reordered child nodes
  >> [alterCopyrightNode]: corrected copyright text
  ..normalized
  ..NORMALIZED XML FAILED VALIDATION
  ..done
[success] Total time: 1 s, completed Sep 20, 2016 3:26:33 PM

看起来Scala RulesTransformer基本上将规则作为Seq并对其进行了.foldLeft,所以我不明白我是如何得到结果的我是&#39我看到了。有什么想法吗?

class RuleTransformer(rules: RewriteRule*) extends BasicTransformer {
  override def transform(n: Node): Seq[Node] =
    rules.foldLeft(super.transform(n)) { (res, rule) => rule transform res }
}

0 个答案:

没有答案