// start with a sample xml 'document'
val xml = <root attr="willIStayOrWillIGoAway"><container/></root>
// create a namespace binding using TopScope as the "parent" argument
val ns = new NamespaceBinding("ns", "http://example.com/ns", TopScope)
// create a second namespace binding, and pass "ns" as the new parent argument
val xsi = new NamespaceBinding("xsi", "http://www.w3.org/2001/XMLSchema-instance", ns)
请注意,我们创建了两个NamespaceBindings,但是因为它们是“链接”在一起的,所以我们只需要将最后一个传递给我们的 RuleTransformer 类。
// the schemaLocation needs to be a PrefixedAttribute
val schemaLoc = new PrefixedAttribute("xsi", "schemaLocation", "http://example.com/ns http://example.com/xml/xsd/xml-schema-articles-v1.0.xsd", Null)
xmlns:ns 和 xmlns:xsi 属性实际上是 NamespaceBindings - 我不知道为什么它们的处理方式不同。但是xsi:schemaLocation 实际上是一个作用域属性,所以我们使用PrefixedAttribute。
// in order to limit the insertion to the root node, you'll need it's label
val rootNodeLabel = "root"
// make a new RewriteRule object
val setSchemaAndNamespaceRule = new setNamespaceAndSchema(rootNodeLabel, xsi, schemaLoc)
// apply the rule with a new transformer
val newxml = new RuleTransformer(setSchemaAndNamespaceRule).transform(xml).head
应用转换器返回 this。
newxml: scala.xml.Node =
<root attr="willIStayOrWillIGoAway"
xmlns:ns="http://example.com/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.com/ns http://example.com/xml/xsd/xml-schema-articles-v1.0.xsd">>
<container/>
</root>
这是返回重写规则的类。
// new class that extends RewriteRule
class setNamespaceAndSchema(rootLabel: String, ns: NamespaceBinding, attrs: MetaData) extends RewriteRule {
// create a RewriteRule that sets this as the only namespace
override def transform(n: Node): Seq[Node] = n match {
// ultimately, it's just a matter of setting the scope & attributes
// on a new copy of the xml node
case e: Elem if(e.label == rootLabel) =>
e.copy(scope = ns, attributes = attrs.append(e.attributes))
case n => n
}
}
注意我们原来的属性被保留了;看看我们扩展的 RewriteRule 类是如何附加 schemaLocation 属性而不是直接使用它的。