有没有办法将孩子(取决于 ID)向后移动两级,进入一个新元素,而不触及所有其他内容?
将带有id="something"
<com>
的节点移动到新节点<ele id="999">
中。如果条件不匹配,则不执行任何操作。
初始 XML:
<lev0>
<lev1>
<ele id="2">
<lev3>
<lev4 id="a_dif_thing">
<com id="">text 1</com>
<com id="">text 2</com>
</lev4>
</lev3>
</ele>
<ele id="1">
<lev3>
<lev4 id="a_thing">
<com id="otherthing">text</com>
<com id="otherthing">text <i>italic</i></com>
<com id="something"><span>text text</span></com>
</lev4>
</lev3>
</ele>
<ele>
<lev3>
<lev4>
<com id="">text</com>
</lev4>
</lev3>
</ele>
</lev1>
</lev0>
输出 XML:
<lev0>
<lev1>
<ele id="2">
<lev3>
<lev4 id="a_dif_thing">
<com id="">text 1</com>
<com id="">text 2</com>
</lev4>
</lev3>
</ele>
<ele id="1">
<lev3>
<lev4 id="a_thing">
<com id="otherthing">text</com>
<com id="otherthing">text <i>italic</i></com>
</lev4>
</lev3>
</ele>
<ele id="999">
<lev3>
<lev4 id="a_new_thing">
<com id="something"><span>text text</span></com>
</lev4>
</lev3>
</ele>
<ele>
<lev3>
<lev4>
<com id="">text</com>
</lev4>
</lev3>
</ele>
</lev1>
</lev0>
示例编辑以显示创建的每个新<ele>
的优选位置。
---根据您的澄清进行了编辑---
试试这样?
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ele[descendant::com[@id='something']]">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<!-- append a new branch to folow this node-->
<ele id="999">
<lev3>
<lev4 id="a_new_thing">
<!-- copy matching nodes -->
<xsl:copy-of select="descendant::com[@id='something']"/>
</lev4>
</lev3>
</ele>
</xsl:template>
<!-- suppress moved nodes -->
<xsl:template match="com[@id='something']"/>
</xsl:stylesheet>
我希望这个样式表有所帮助:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>
<!-- Identity transform template to copy all nodes and attributes -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- template to process 4th ancestor of com[@id = 'something'] -->
<xsl:template match="*[*/*/*/com[@id = 'something']]">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:apply-templates select="*/*/*/com[@id = 'something']" mode="special"/>
</xsl:copy>
</xsl:template>
<!-- create the element when com[@id = 'something'] is encountered -->
<xsl:template match="com" mode="special">
<ele id="999">
<lev3>
<lev4 id="a_new_thing">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</lev4>
</lev3>
</ele>
</xsl:template>
<!-- do nothing for com[@id = 'something'] elements -->
<xsl:template match="*/*/*/*/com[@id = 'something']"/>
</xsl:stylesheet>