【发布时间】:2016-08-10 04:41:23
【问题描述】:
我正在尝试使用 XSL 文件将 XML 属性的特定元素移动到特定位置。
假设我有 XML
<all>
<one>Something 1</one>
<check>
<present>
<target> Hello </target>
<abc> ABC </abc>
</present>
<absent>
<target> Hi </target>
<pqr> PQR </pqr>
</absent>
</check>
<action>
<perform>
<one>One</one>
<two>Two</two>
</perform>
</action>
</all>
我想要这样的输出:
<all>
<one>Something 1</one>
<check>
<present>
<abc> ABC </abc>
</present>
<absent>
<target> Hi </target>
<pqr> PQR </pqr>
</absent>
</check>
<action>
<perform>
<one>One</one>
<two>Two</two>
<target> Hello </target>
</perform>
</action>
</all>
为此我写了
<xsl:template match= "@*|node()" >
<xsl:copy>
<xsl:apply-templates select= "@*|node()" />
</xsl:copy>
</xsl:template >
<xsl:template match= "action" >
<xsl:copy>
<xsl:apply-templates select= "@*|node()" />
<xsl:apply-templates select= "../check/present/target" mode= "move" />
</xsl:copy>
</xsl:template >
<xsl:template match="target" mode="move">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<target>Hello</target> 移动到 <action> 下,但我希望它移动到 <perform> 内部 <action> 内部
【问题讨论】:
-
你也有mode="move"的模板匹配目标吗?
-
好吧,应用一个不存在的模板不会做任何事情。请参阅此处的示例:stackoverflow.com/questions/38650232/… 请注意,如果您不对“已移动”节点进行任何更改,则可以使用
xsl:copy。 -
@michael.hor257k 我更新了我的 xsl 现在我有一个模板匹配目标与 mode="move"
-
它完成了
<xsl:apply-templates select= "../../check/present/target" mode= "move" />