问题描述
我有一个看起来像这样的XML
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>
如您所见,varList元素需要嵌套在simpleTags元素内,紧接在开始标记之后. simpleSteps中可能还有其他varList元素,不能更改.
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.
有什么想法可以使用XSLT实现吗?我是XSLT的新手,一整天都徒劳地尝试了...任何帮助将不胜感激.
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
推荐答案
以下样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="varList[following-sibling::*[1][self::simpleSteps]]" />
<xsl:template match="simpleSteps">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:copy-of select="preceding-sibling::varList[1]" />
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
在此输入上:
<executionPlan name="Test">
<paramList>
<param name="param1" default="" />
</paramList>
<varList>
<var name="bla" default=":[param1]" />
</varList>
<varList>
<var name="bla2" default=":[param2]" />
</varList>
<simpleSteps limitToHostSet="bla">
<execNative>
<exec cmd="/bin/sh" />
</execNative>
</simpleSteps>
</executionPlan>
产生:
<executionPlan name="Test">
<paramList>
<param name="param1" default="" />
</paramList>
<varList>
<var name="bla" default=":[param1]" />
</varList>
<simpleSteps limitToHostSet="bla">
<varList>
<var name="bla2" default=":[param2]" />
</varList>
<execNative>
<exec cmd="/bin/sh" />
</execNative>
</simpleSteps>
</executionPlan>
:仅将紧前面的varList
移到与其关联的simpleSteps
中.所有其他varList
元素都将通过原样复制.
Only the immediately preceding varList
is moved into its associated simpleSteps
. All other varList
elements are copied through unchanged.
突然之间,我不清楚这是否是所需的行为,或者simpleSteps
元素中是否已经存在多个应该保持不变的varList
元素.对于这种情况,请参阅我的原始解决方案:
It's suddenly not clear to me whether this is the desired behavior, or if there may be multiple varList
elements already inside the simpleSteps
element that should be unchanged. See my original solution for that case:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="varList" />
<xsl:template match="simpleSteps">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:copy-of select="../varList" />
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
在此输入上:
<executionPlan name="Test">
<paramList>
<param name="param1" default="" />
</paramList>
<varList>
<var name="bla" default=":[param1]" />
</varList>
<simpleSteps limitToHostSet="bla">
<varList>
<var name="bla7" default=":[param7]" />
</varList>
<execNative>
<exec cmd="/bin/sh" />
</execNative>
</simpleSteps>
</executionPlan>
产生:
<executionPlan name="Test">
<paramList>
<param name="param1" default="" />
</paramList>
<simpleSteps limitToHostSet="bla">
<varList>
<var name="bla" default=":[param1]" />
</varList>
<varList>
<var name="bla7" default=":[param7]" />
</varList>
<execNative>
<exec cmd="/bin/sh" />
</execNative>
</simpleSteps>
</executionPlan>
这篇关于使用xslt将xml元素移动到另一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!