首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT -追加同级

XSLT -追加同级
EN

Stack Overflow用户
提问于 2012-04-19 19:46:43
回答 1查看 2.3K关注 0票数 2

我正在尝试使用XSLT将context-param附加为最后一个兄弟。没有公共的父元素,所以任务有点难。

我想附加以下元素:

代码语言:javascript
复制
<context-param>
    <param-name>miku</param-name>
    <param-value>kawaii</param-value>
</context-param>

作为最后一个context-param元素(例如,所有context-param元素必须彼此相邻,它们不能分散在下面的xml中的任何位置:

代码语言:javascript
复制
<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>

结果应该如下所示:

代码语言:javascript
复制
<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>

我该怎么做呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-19 20:20:23

此转换:

代码语言:javascript
复制
<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文档时的

代码语言:javascript
复制
<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>

会生成想要的正确结果:

代码语言:javascript
复制
<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".

  • There
  1. 身份规则复制每个节点“as-is”.
  2. There是单个模板,覆盖身份模板。此模板匹配所有context-param元素的最后一个context-param元素,这些元素是其父元素的子元素。
  3. 在覆盖模板中,执行两个操作;通过调用标识规则复制当前节点;然后将要附加的元素复制到输出。为了方便和灵活起见,我们假设要追加的元素作为参数传递给transformation.
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10227347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档