MarkupBuilder – 生成不带封装的属性

huangapple go评论55阅读模式
英文:

MarkupBuilder - Generate attributes without encapsulation

问题

以下是您要翻译的部分:

但它生成了:

<consolidate amount="220.36" date="2023-01-06" invoiceId="123456XXX" currency="EUR" />
</consolidate>

而期望的是:

<consolidate amount=220.36 date="2023-01-06" invoiceId="123456XXX" currency="EUR" />
</consolidate>
英文:

Is it possible to generate attributes using MarkupBuilder() without encapsulating it. I have the following:


def writer = new StringWriter()
def xml = new MarkupBuilder(writer)

// generate strings with double quotes
xml.setDoubleQuotes(true);


invoiceId = "123456XXX";
date = "2023-01-06";
amount = 220.36;
  
xml.consolidate(amount:amount.toDouble(),
					date: date,
					invoiceId: invoiceId,
					currency:'EUR');
  
}

but it generates:

<consolidate amount="220.36" date="2023-01-06" invoiceId="123456XXX" currency="EUR" />
</consolidate>

when the following is expected:

 <consolidate amount=220.36 date="2023-01-06" invoiceId="123456XXX" currency="EUR" />
</consolidate>

答案1

得分: 1

你标记为“expected”的结果不是有效的XML(可以使用验证器进行检查)。属性的值必须始终用引号括起来:

> 属性值必须始终用引号括起来。可以使用单引号或双引号。

请参阅此处了解更多信息。

由于这个原因,你不应该,也可能无法使MarkupBuilder生成所期望的结果。

英文:

The result that you put as "expected" is not a valid XML (you may check it with the validator). The attribute's value is always quoted:

> Attribute values must always be quoted. Either single or double quotes
> can be used.

See more here.

For that reason you shouldn't and probably can't make MarkupBuilder to produce the desired result.

huangapple
  • 本文由 发表于 2023年6月5日 20:34:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定