我正在尝试使用XSLT追加context-param
作为最后一个兄弟。没有共同的父元素,所以任务有点困难。
我想追加以下元素:
<context-param>
<param-name>miku</param-name>
<param-value>kawaii</param-value>
</context-param>
作为最后一个context-param
元素(例如,所有context-param
元素必须彼此相邻,它们不能分散在xml中的任何位置):
<web-app>
<not_interesting_element1/>
<not_interesting_element2/>
<context-param>
<param-name>not_interesting_param_key1</param-name>
<param-value>kawaii</param-value>
</context-param>
<context-param>
<param-name>not_interesting_param_key2</param-name>
<param-value>kawaii</param-value>
</context-param>
<context-param>
<param-name>parameterThatsGuaranteedToBeHere</param-name>
<param-value>someValue</param-value>
</context-param>
<not_interesting_element3/>
<not_interesting_element4/>
<!-- ... servlets, ... -->
</web-app>
结果应如下所示:
<web-app>
<not_interesting_element1/>
<not_interesting_element2/>
<context-param>
<param-name>not_interesting_param_key1</param-name>
<param-value>kawaii</param-value>
</context-param>
<context-param>
<param-name>not_interesting_param_key2</param-name>
<param-value>kawaii</param-value>
</context-param>
<context-param>
<param-name>parameterThatsGuaranteedToBeHere</param-name>
<param-value>someValue</param-value>
</context-param>
<context-param>
<param-name>miku</param-name>
<param-value>kawaii</param-value>
</context-param>
<not_interesting_element3/>
<not_interesting_element4/>
<!-- ... servlets, ... -->
</web-app>
我怎么能这样做?
答案 0 :(得分:2)
此转化:
<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="pElemToAdd">
<context-param>
<param-name>miku</param-name>
<param-value>kawaii</param-value>
</context-param>
</xsl:param>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="context-param[last()]">
<xsl:call-template name="identity"/>
<xsl:copy-of select="$pElemToAdd"/>
</xsl:template>
</xsl:stylesheet>
应用于提供的XML文档时:
<web-app>
<not_interesting_element1/>
<not_interesting_element2/>
<context-param>
<param-name>not_interesting_param_key1</param-name>
<param-value>kawaii</param-value>
</context-param>
<context-param>
<param-name>not_interesting_param_key2</param-name>
<param-value>kawaii</param-value>
</context-param>
<context-param>
<param-name>parameterThatsGuaranteedToBeHere</param-name>
<param-value>someValue</param-value>
</context-param>
<not_interesting_element3/>
<not_interesting_element4/>
<!-- ... servlets, ... -->
</web-app>
会产生想要的正确结果:
<web-app>
<not_interesting_element1/>
<not_interesting_element2/>
<context-param>
<param-name>not_interesting_param_key1</param-name>
<param-value>kawaii</param-value>
</context-param>
<context-param>
<param-name>not_interesting_param_key2</param-name>
<param-value>kawaii</param-value>
</context-param>
<context-param>
<param-name>parameterThatsGuaranteedToBeHere</param-name>
<param-value>someValue</param-value>
</context-param>
<context-param>
<param-name>miku</param-name>
<param-value>kawaii</param-value>
</context-param>
<not_interesting_element3/>
<not_interesting_element4/><!-- ... servlets, ... -->
</web-app>
<强>解释强>:
身份规则“按原样”复制每个节点。
有一个模板,覆盖身份模板。此模板与作为其父级子级的所有context-param
元素的最后一个context-param
元素匹配。
在覆盖模板中执行两个操作;通过调用身份规则复制当前节点;然后将要追加的元素复制到输出中。为了方便和灵活,我们假设要附加的元素作为参数传递给转换。