0
異なるXMLファイルの束を正規化するために、ラッパー要素内に子要素をラップするXML RewriteRuleを記述しようとしています。これは私が必要とするものですが、私はElemで終わり、transform()
関数をオーバーライドするには、NodeSeqを返す必要があります。Scala ElemをNodeSeqにキャストする方法
/* WRAP NODES INSIDE PARENT NODE
*
* Some Nodes should be wrapped in an enclosing Node. For example, some xml versions
* of the article <contentitem> have <category> as a direct child, but category info
* should really have this structure:
* <categories>
* <category>some category name</category>
* </categories>
*/
val wrapIndividualNodes = new RewriteRule {
val nodesToWrap = List(("category", "categories"), ("subcategory", "subcategories"))
override def transform(n: Node): NodeSeq = {
for ((target, wrapper) <- nodesToWrap) {
// copy, then delete, children
val categoryNodes = (n \ target)
val articleWithoutCategoryNodes = SharedRules.deleteChildren(n, target)
// wrap children and add as new child
val categories = <categories>{categoryNodes}</categories>
SharedRules.addChild(articleWithoutCategoryNodes, categories)
}
}
}
SharedRules.scala
/**
* Adds newChild as the last element in the parent node
*/
def addChild(parent: Node, child: Node): Elem = {
parent.copy(child = parent.child ++ child)
}
/**
* Deletes all children with matching childLabel from parent
*/
def deleteChildren(parent: Node, childLabel: String): Elem = {
parent.copy(child = parent.child.filterNot(_.label == childLabel))
}
hh。私はAPIのようなものを探していましたが、何も見つかりませんでした:http://www.scala-lang.org/api/2.11.1/scala-xml/#scala.xml.NodeSeq – doub1ejack
私の答えを読み返すElemはNodeSeqを拡張するNodeであるため、期待したことができないかもしれないことに気づきます。したがって、 'NodeSeq.fromSeq(elem)'は 'elem'と等価です。私はあなたが最初にシングルシークレットでそれを包み込むと信じています。 –
'elem.theSeq'を使うと、' Seq [Node] 'が暗黙的に' NodeSeq'に変換されることに気付きました –