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

查看:21
本文介绍了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>

结果应该是这样的:

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

请问我该怎么做?

推荐答案

这种转变:

<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天全站免登陆