您可以使用XSLT来完成。然后只需使用生成所需的代码片段。
(记住将输出格式设置为纯文本)
编辑:示例XSLT脚本
输入-A.XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="b.xsl"?>
<set name='myVar'>
<concat>
<s>newText_</s>
<ref>otherVar</ref>
</concat>
</set>
脚本-B.XSL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="text" />
<xsl:template match="set">
<xsl:value-of select="@name"/>=<xsl:apply-templates/>
</xsl:template>
<xsl:template match="concat">
<xsl:for-each select="*">
<xsl:if test="position() > 1">+</xsl:if>
<xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:template>
<xsl:template match="ref">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="s">
<xsl:text>"</xsl:text>
<xsl:apply-templates/>
<xsl:text>"</xsl:text>
</xsl:template>
</xsl:stylesheet>
请注意,a.xml包含一条指令,允许支持XSLT的浏览器使用样式表b.xsl呈现它。火狐就是这样一个浏览器。在firefox中打开a.xml,您将看到
myVar="newText_"+otherVar
请注意,XSLT是一种非常强大的编程语言,因此您可以做很多事情。