使用 Groovy+Smooks 将命名空间声明添加到现有 XML

发布于 2024-10-19 02:25:31 字数 733 浏览 3 评论 0原文

我正在编写这个在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甚是思念 2024-10-26 02:25:31

尝试按如下所示格式化元素:

 "elementname"('xsi:nil' : 'true', 'xmlns:xsi' : 'http://www.w3.org/2001/XMLSchema-instance')

这将返回以下 XML 标记:

<elementname xsi:nil='true' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />

希望这可以解决您的问题。

try formatting the element as shown below:

 "elementname"('xsi:nil' : 'true', 'xmlns:xsi' : 'http://www.w3.org/2001/XMLSchema-instance')

this will return the following XML tag:

<elementname xsi:nil='true' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />

Hope this solves your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文