XSLT - 追加兄弟 [英] XSLT - append sibling

查看:148
本文介绍了XSLT - 追加兄弟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用XSLT追加的context-param 在去年的兄弟姐妹。有没有共同的父元素这样的任务是有点困难。

I'm trying to use XSLT to append context-param as last sibling. There's no common parent element so the task is a little bit harder.

我想追加以下元素:

<context-param>
    <param-name>miku</param-name>
    <param-value>kawaii</param-value>
</context-param>

作为最后的的context-param 元素(例如,所有的的context-param 元素必须是彼此相邻,它们不能在任何地方以xml在下面的XML散):

as the last context-param element (eg. all context-param elements must be next to each other, they can't be scattered anywhere in xml) in following 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>

结果应该是这样的:

The result should look like this:

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

如何能做到这一点吗?

How could I do it please?

推荐答案

这样的转换:

<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. 身份规则副本的每个节点原样。

  1. The identity rule copies every node "as-is".

有一个模板,覆盖身份模板。此模板是其父母的子女均的context-param 元素的最后一个的context-param 元素相匹配。

There is a single template, overriding the identity template. This template matches the last context-param element of all context-param elements that are children of their parent.

在压倒一切的模板执行两个动作;当前节点是通过对身份规则调用复制;然后该元件要追加被复制到输出。为便利性和灵活性,我们假设要追加的元件被作为一个参数到变换传递

In the overriding template two actions are performed; the current node is copied by a call to the identity rule; then the element to be appended is copied to the output. For convenience and flexibility, we assume that the element to be appended is passed as a parameter to the transformation.

这篇关于XSLT - 追加兄弟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆