使用 Groovy+Smooks 将命名空间声明添加到现有 XML
我正在编写这个在 Smooks 内运行并编辑 XML 的 groovy 脚本。我试图将 xsi:nil="true" 添加到空字段,但我遇到的问题是 xsi 命名空间声明未添加到根字段。这是我尝试过的:
element['@xsi:nil'] = 'true'
并且
def nil = doc.createAttributeNS("http://www.w3.org/2001/XMLSchema-instance", 'xsi:nil')
nil.setValue('true');
element.setAttributeNodeNS(nil)
这两个都会添加属性,但不会添加 NS 声明。
我也尝试过这个,但它会导致堆栈溢出错误:
def doc = element.getOwnerDocument();
doc.declareNamespace(xsi: 'http://www.example.org/xsi')
这是 Smooks 文档的链接: http://www.smooks.org/mediawiki/index.php ?title=V1.4:groovy
基本上,该脚本在运行时生成的 groovy 类内运行。
I'm writing this groovy script that runs inside Smooks and edits an XML. I'm trying to add xsi:nil="true" to the empty fields and the problem I'm having is that the xsi namespace declaration isn't added to the root field. Here's what I tried:
element['@xsi:nil'] = 'true'
and
def nil = doc.createAttributeNS("http://www.w3.org/2001/XMLSchema-instance", 'xsi:nil')
nil.setValue('true');
element.setAttributeNodeNS(nil)
Both of these will add the attribute but not the NS declaration.
I also tried this but it results in a stack overflow error:
def doc = element.getOwnerDocument();
doc.declareNamespace(xsi: 'http://www.example.org/xsi')
Here's the link to the Smooks documentation:
http://www.smooks.org/mediawiki/index.php?title=V1.4:groovy
Basically the script runs inside a groovy class that is generated at runtime.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试按如下所示格式化元素:
这将返回以下 XML 标记:
希望这可以解决您的问题。
try formatting the element as shown below:
this will return the following XML tag:
Hope this solves your problem.