xml - 使用 XSLT 将父元素添加到不同的 XML 同级
问题描述
我有一个关于 XSLT 2.0 的问题。我处于这样的情况:
<config>
<!-- elements that needs to be copied as they are -->
<a>
<childA attr="attr">text</childA>
</a>
<b>
<childB attrb="attr">text B</childB>
</b>
<c>
<childC attrc="attr">text</childC>
</c>
</config>
我想要这个:
<config>
<!-- elements that needs to be copied as they are -->
<parent>
<a>
<childA attr="attr">text</childA>
</a>
<b>
<childB attrb="attr">text B</childB>
</b>
<C>
<childC attrc="attr">text</childC>
</c>
</parent>
</config>
我做了很多尝试,但它们基本上都是错误的,我也尝试使用 for-each-group 但绝对我不认为那是方法......你能给我一些关于如何/在哪里寻找的提示吗?
太感谢了!
解决方案
为config
<xsl:template match="config">
<xsl:copy>
<xsl:apply-templates select="* except (a, b, c)"/>
<parent>
<xsl:apply-templates select="a, b, c"/>
</parent>
</xsl:copy>
</xsl:template>
连同恒等变换为起点(即<xsl:mode on-no-match="shallow-copy"/>
或同拼写为模板)。
推荐阅读
- angular - 模板引用变量:分配 ngForm 和 ngModel
- android - 通过 ACTION_VIEW 意图将图像从内部存储保存到图库
- c - fdim 的首字母缩略词代表什么?
- javascript - javascript如何将“空”插入数组?
- android-studio - 尝试在 Android Studio 3.5.3 中创建新项目时出现很多错误
- vb.net - 在文件夹日期上使用获取文本后,如何获得正确的日期格式?
- javascript - 切换数组值的最快方法
- node.js - 如何在谷歌云功能中正确路由所有 nodejs express 应用程序请求
- java - System.out.println("Serializable:" + arrayList instanceof Serializable) 没有打印 'Serializable' 字
- wordpress - 在 WooCommerce 订单中创建自定义元信息