【问题标题】:Scala RewriteRules to set namespace & schemaLocation?Scala RewriteRules 设置命名空间和模式位置?
【发布时间】:2016-09-10 04:50:58
【问题描述】:

我正在生成一个 xml 文件并想创建一些转换 RewriteRules,它将以下内容插入到根元素中:

<content
  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">

看起来from this gist 设置xmls:ns 命名空间是创建一个NamespaceBinding 并将其应用为Elem 范围的问题。但是,我还没有为此成功创建 RewriteRule,我仍在寻找如何添加架构实例 (xmlns:xsi) 和 schemaLocation。

【问题讨论】:

  • 您是如何生成 XML 的?
  • 我实际上是从一个文件中读取旧的 xml 并使用一堆 scala.xml.transform RewriteRules 来修改它。

标签: xml scala schema


【解决方案1】:
// 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:nsxmlns: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 属性而不是直接使用它的。

【讨论】:

    猜你喜欢
    • 2010-12-15
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2022-07-10
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    相关资源
    最近更新 更多