使用 xslt 将 xml 元素移动到另一个元素
我有一个如下所示的 XML
<executionPlan name="Test" >
<paramList>
<param name="param1" default=""/>
</paramList>
<varList>
<var name="bla" default=":[param1]"/>
</varList>
<simpleSteps limitToHostSet="bla">
<execNative>
<exec cmd="/bin/sh"/>
</execNative>
</simpleSteps>
,我需要将其转换为如下所示:
<executionPlan name="Test" >
<paramList>
<param name="param1" default=""/>
</paramList>
<simpleSteps limitToHostSet="bla">
<varList>
<var name="bla" default=":[param1]"/>
</varList>
<execNative>
<exec cmd="/bin/sh"/>
</execNative>
</simpleSteps>
正如您所看到的,varList 元素需要嵌套在紧跟在开始标记后面的 simpleSteps 元素内。 simpleSteps 内可能还有其他不得更改的 varList 元素。
有什么想法如何使用 XSLT 实现这一点吗?我是 XSLT 的新手,尝试了一整天都是徒劳……任何帮助将不胜感激。
卢茨
I have an XML that looks like this
<executionPlan name="Test" >
<paramList>
<param name="param1" default=""/>
</paramList>
<varList>
<var name="bla" default=":[param1]"/>
</varList>
<simpleSteps limitToHostSet="bla">
<execNative>
<exec cmd="/bin/sh"/>
</execNative>
</simpleSteps>
and I need to transform it to look like this:
<executionPlan name="Test" >
<paramList>
<param name="param1" default=""/>
</paramList>
<simpleSteps limitToHostSet="bla">
<varList>
<var name="bla" default=":[param1]"/>
</varList>
<execNative>
<exec cmd="/bin/sh"/>
</execNative>
</simpleSteps>
As you can see the varList element needs to be nested inside the simpleSteps element immediately behind the opening tag. There may be other varList elements inside simpleSteps which must not be changed.
Any ideas how to achieve that with XSLT? I am a newby to XSLT and tried the whole day in vain... Any help would really be appreciated.
Lutz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下样式表:
在此输入上:
生成:
编辑: 仅将前面的
varList
移动到其关联的simpleSteps
中。所有其他varList
元素均按原样复制。我突然不清楚这是否是所需的行为,或者
simpleSteps
元素中是否已经存在多个应该保持不变的varList
元素。请参阅我针对该情况的原始解决方案:在此输入上:
产生:
The following style sheet:
On this input:
Produces:
Edit: Only the immediately preceding
varList
is moved into its associatedsimpleSteps
. All othervarList
elements are copied through unchanged.It's suddenly not clear to me whether this is the desired behavior, or if there may be multiple
varList
elements already inside thesimpleSteps
element that should be unchanged. See my original solution for that case:On this input:
Produces:
这是一个更简单、更简短的解决方案:
当应用于提供的 XML 文档时:
产生了所需的正确结果:
解释:
身份规则< /strong>/template “按原样”复制每个节点。只有两种例外情况,如下所述。
仅与顶部元素的第一个
varList
子元素匹配的覆盖模板没有主体——这实际上取消了该元素的标识模板的复制操作。与
simpleSteps
的第一个子元素匹配的重写模板会执行两件事:a) 复制所需的varList
(顶部元素的子元素)元素,然后 b )它调用身份模板将其自身复制到输出。This is a simpler and shorter solution:
when applied on the provided XML document:
exactly the wanted, correct result is produced:
Explanation:
The identity rule/template copies every node "as-is". There are only two exceptions, explained below.
The overriding template that matches only the first
varList
child of the top element has no body -- this effectively annuls the copying action of the identity template for this element.The overriding template that matches the first element child of
simpleSteps
does two things: a) it copies the wantedvarList
(child of the top element) element and then b) it calls the identity template to copy itself to the output.