我正在尝试使用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>我该怎么做呢?
发布于 2012-04-19 20:20:23
此转换:
<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>说明
"as-is".
context-param元素的最后一个context-param元素,这些元素是其父元素的子元素。https://stackoverflow.com/questions/10227347
复制相似问题