我正在使用 groovy 和 groovy.xml.MarkupBuilder 编写一个库,我将用它来自动创建 epub 文件。我正在编写一个可用于生成 container.xml 文件的函数,我注意到当我尝试使用带有连字符的属性名称时,我的 IDE 会给出一个错误,而当属性名称没有连字符时,我会发现这个错误连字符。
def writer = new StringWriter();
def xml = new MarkupBuilder(writer);
def fullPath="full-path"
def mediaType="media-type"
def generateContainer()
{
xml.xmlDeclaration()
xml.container(version:'1.0', xmlns:'urn:oasis:names:tc:opendocument:xmlns:container')
{
rootfiles
{
rootfile( this.fullPath:'OEBPS/content.opf',this.mediaType:'application/oebps-package+xml')
}
}
}
当我尝试使用
full-path
我收到一个错误。当我尝试使用
fullpath
它不会给我错误。
为什么会发生这种情况以及如何纠正它?请参阅here
The value of full-path (in bold) is the only part of this file that will ever vary.
我希望尽可能准确
最佳答案
这按预期工作:
import groovy.xml.MarkupBuilder
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
def fullPath = "full-path"
def mediaType = "media-type"
xml.mkp.xmlDeclaration( version: "1.0", encoding: "utf-8" )
xml.container( version:'1.0',
xmlns:'urn:oasis:names:tc:opendocument:xmlns:container' ) {
rootfiles {
rootfile( (fullPath) : 'OEBPS/content.opf',
(mediaType): 'application/oebps-package+xml' )
}
}
println writer
确保在引用变量 fullPath
时,必须使用 (fullPath)
[大括号],以便将变量的值用作属性xml 节点。
关于xml - 如何对名称中包含连字符的属性使用 groovy XMLbuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23207234/