XSLT 解决方案:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pNewIpAddress" select="'192.68.0.1'"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ipAddress/text()">
<xsl:value-of select="$pNewIpAddress"/>
</xsl:template>
</xsl:stylesheet>
ipAddress
当此转换应用于任何文档时,文档的所有节点都“按原样”复制,但任何元素的文本节点子节点除外(无论此元素在文档中的什么位置)。后者被替换为外部提供的参数的值,名为$pNewIpAddress
。
例如,如果针对此 XML 文档应用转换:
<t>
<a>
<b>
<ipAddress>127.0.0.1</ipAddress>
</b>
<c/>
</a>
<d/>
</t>
产生了想要的正确结果:
<t>
<a>
<b>
<ipAddress>192.68.0.1</ipAddress>
</b>
<c/>
</a>
<d/>
</t>
有许多基于 Java 的 XSLT 处理器,了解如何从 Java 调用它们的适当位置是它们的文档。最好的此类 XSLT 处理器之一是 Saxon,其文档可在以下位置找到:
http://www.saxonica.com/documentation/documentation.xml