val sitemapXml = <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>
如何以编程方式将xmlns:image添加到其中以获取
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"></urlset>
我有条件地需要它,否则我会写它。
答案 0 :(得分:0)
命名空间位于scope
Elem
scala> sitemapXml.scope
res0: scala.xml.NamespaceBinding = xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
这是添加新命名空间的方法
scala> val newNs = scala.xml.NamespaceBinding("image", "http://www.google.com/schemas/sitemap-image/1.1", sitemapXml.scope)
newNs: scala.xml.NamespaceBinding = xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
然后使用copy
构造函数修改XML:
scala> sitemapXml.copy(scope = newNs)
res1: scala.xml.Elem = <urlset xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>