XSLT - 追加兄弟姐妹

时间:2012-04-19 11:46:43

标签: xml xslt append flat siblings

我正在尝试使用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>

我怎么能这样做?

1 个答案:

答案 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>

<强>解释

  1. 身份规则“按原样”复制每个节点。

  2. 有一个模板,覆盖身份模板。此模板与作为其父级子级的所有context-param元素的最后一个context-param元素匹配。

  3. 在覆盖模板中执行两个操作;通过调用身份规则复制当前节点;然后将要追加的元素复制到输出中。为了方便和灵活,我们假设要附加的元素作为参数传递给转换。