问题描述
基本上,我们正在改变InDesign项目的数据源XML方式。我们不想更改InDesign,所以我必须从新的数据源(通过Mule ESB提供的SOAP web服务)调整新的XML。我使用groovy来转换所说的XML,直到我遇到这个问题。 InDesign中的旧XML仅适用于每个文本之后添加的特殊字符:

。 下面是一个可用的XML示例:
<?xml version ='1.0'encoding ='UTF-8'? >
< w_import_saisie_web>< w_evenement>< w_titre>我的标题&#xA;< / w_titre>< / w_evenement>
< / w_import_saisie_web>
我无法在Groovy脚本中添加特殊字符:以下是我到目前为止所尝试的内容: / p>
root = new XmlSlurper(false,false).parseText(有效内存)
def xml = new StringWriter ().with {w - > new groovy.xml.MarkupBuilder(w).with {
mkp.xmlDeclaration(version:1.0,encoding:utf-8)
w_import_saisie_web(){
w_titre (root.title +&#xA;)
}
}
}
w.toString()
}
$
我也试过: (root.title +'& amp'+'#xA;')
w_titre(root。w_titre(root.title +'&#xA;')
w_titre title +& amp+#xA;)
etc
我可以毫无问题地打印此内容。
<?xml version ='1.0'编码= 'UTF-8' >?;
< w_import_saisie_web>< w_evenement>< w_titre>我的标题< / w_titre>< / w_evenement>
< / w_import_saisie_web>
但我似乎无法这样做:
<?xml version ='1.0'encoding ='UTF-8'?>
< w_import_saisie_web>< w_evenement>< w_titre>我的标题&#xA;< / w_titre>< / w_evenement>
< / w_import_saisie_web>
有人可以帮我吗?
您可以使用 mkp.yieldUnescaped
:
println new StringWriter()。with {w - >
新增了groovy.xml.MarkupBuilder(w).with {
escapeAttributes = false
mkp.xmlDeclaration(版本:1.0,编码:utf-8)
w_import_saisie_web {
w_titre {
mkp.yieldUnescaped'woo& #xA;'
}
}
}
w.toString()
}
打印:
<?xml version ='1.0'encoding ='utf-8'?>
< w_import_saisie_web>
< w_titre> woo&#xA;< / w_titre>
< / w_import_saisie_web>
如果您不想硬编码&#xA;
,您可以从 XmlUtil
使用 escapeControlCharacters
:
import groovy.xml.XmlUtil
println new StringWriter()。with {w - >
新增了groovy.xml.MarkupBuilder(w).with {
escapeAttributes = false
mkp.xmlDeclaration(版本:1.0,编码:utf-8)
w_import_saisie_web {
w_titre {
mkp.yieldUnescaped XmlUtil.escapeControlCharacters('woo\\\
')
}
}
}
w.toString()
打印:
<?xml version ='1.0'encoding ='utf-8'?>
< w_import_saisie_web>
< w_titre> woo&#10;< / w_titre>
< / w_import_saisie_web>
Basically we are changing the datasource XML for an InDesign project the way the old. We don't want to change the InDesign so I have to adapt the new XML from the new datasource (a SOAP webservice via Mule ESB). I use groovy to convert said XML, all was going until I faced this issue. The old XML in InDesign only works with this special character added to after every text: 

.
Here's an example of a working XML :
<?xml version='1.0' encoding='UTF-8'?>
<w_import_saisie_web><w_evenement><w_titre>My Title
</w_titre></w_evenement>
</w_import_saisie_web>
I am unable to add the special character in the Groovy script : Here's what I tried so far :
root = new XmlSlurper(false,false).parseText(payload)
def xml = new StringWriter().with { w -> new groovy.xml.MarkupBuilder(w).with {
mkp.xmlDeclaration(version: "1.0", encoding: "utf-8")
"w_import_saisie_web"() {
"w_titre"( root.title + "
")
}
}
}
w.toString()
}
I also tried :
"w_titre"( root.title + '
')
"w_titre"( root.title + '&'+'#xA;')
"w_titre"( root.title + "&"+"#xA;")
etc
I can print this with no problem
<?xml version='1.0' encoding='UTF-8'?>
<w_import_saisie_web><w_evenement><w_titre>My Title</w_titre></w_evenement>
</w_import_saisie_web>
But I can't seem to do this :
<?xml version='1.0' encoding='UTF-8'?>
<w_import_saisie_web><w_evenement><w_titre>My Title
</w_titre></w_evenement>
</w_import_saisie_web>
Can someone help me out?
You can use mkp.yieldUnescaped
:
println new StringWriter().with { w ->
new groovy.xml.MarkupBuilder(w).with {
escapeAttributes = false
mkp.xmlDeclaration(version: "1.0", encoding: "utf-8")
w_import_saisie_web {
w_titre {
mkp.yieldUnescaped 'woo
'
}
}
}
w.toString()
}
Which prints:
<?xml version='1.0' encoding='utf-8'?>
<w_import_saisie_web>
<w_titre>woo
</w_titre>
</w_import_saisie_web>
And if you don't want to hardcode the 

, you can use escapeControlCharacters
from XmlUtil
:
import groovy.xml.XmlUtil
println new StringWriter().with { w ->
new groovy.xml.MarkupBuilder(w).with {
escapeAttributes = false
mkp.xmlDeclaration(version: "1.0", encoding: "utf-8")
w_import_saisie_web {
w_titre {
mkp.yieldUnescaped XmlUtil.escapeControlCharacters('woo\n')
}
}
}
w.toString()
}
Which prints:
<?xml version='1.0' encoding='utf-8'?>
<w_import_saisie_web>
<w_titre>woo </w_titre>
</w_import_saisie_web>
这篇关于如何添加& #xA; Groovy MarkupBuilder中的XML元素中的文本之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!