[英]XSLT 2.0 - Move existing child nodes to newly created Node
<xsl:template match="BaseNode">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates/>
<None attr1="0" attt2="1" attr2="0">
<xsl:apply-templates select="//BaseNode/BasicDouble[@Descr = 'Input A']"/>
<xsl:apply-templates select="//BaseNode/BasicDouble[@Descr = 'Input B']"/>
</None>
</xsl:copy>
</xsl:template>
上面是我嘗試用於將現有子節點移動到新創建的Node的XSLT。 該規則將創建新節點,並將節點復制為子節點。 問題是我在父節點中仍然有舊的Elements。 我要的是剪切和粘貼操作,而不是復制粘貼。 預先感謝您的幫助。
輸入XML
<ParentNode attr1="1" attr2="2">
<BasicInt attr1="1" attr2="2"/>
<BasicInt attr1="1" attr2="2"/>
<BasicDouble attr1="1" attr2="2"/>
<BasicEnum attr1="1" attr2="2"/>
<Pair Key="0" Value="a"/>
<Pair Key="1" Value="b"/>
<Pair Key="2" Value="c"/>
<Pair Key="3" Value="d"/>
</BasicEnum>
<BasicDouble attr1="1" attr2="2"/>
<BasicDouble attr1="1" attr2="2"/>
</ParentNode>
期望的輸出
<ParentNode attr1="1" attr2="2">
<BasicInt attr1="1" attr2="2"/>
<BasicInt attr1="1" attr2="2"/>
<BasicDouble attr1="1" attr2="2"/>
<BasicEnum attr1="1" attr2="2"/>
<Pair Key="0" Value="a"/>
<Pair Key="1" Value="b"/>
<Pair Key="2" Value="c"/>
<Pair Key="3" Value="d"/>
</BasicEnum>
<NewElement>
<BasicDouble attr1="1" attr2="2"/>
<BasicDouble attr1="1" attr2="2"/>
</NewElement>
</ParentNode>
用我的規則獲得輸出
<ParentNode attr1="1" attr2="2">
<BasicInt attr1="1" attr2="2"/>
<BasicInt attr1="1" attr2="2"/>
<BasicDouble attr1="1" attr2="2"/>
<BasicEnum attr1="1" attr2="2"/>
<Pair Key="0" Value="a"/>
<Pair Key="1" Value="b"/>
<Pair Key="2" Value="c"/>
<Pair Key="3" Value="d"/>
</BasicEnum>
<BasicDouble attr1="1" attr2="2"/>
<BasicDouble attr1="1" attr2="2"/>
<NewElement>
<BasicDouble attr1="1" attr2="2"/>
<BasicDouble attr1="1" attr2="2"/>
</NewElement>
請提供XML示例以進行更清晰的說明。 您可以嘗試以下方法:
<xsl:template match="BaseNode">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates select="node() except (//BaseNode/BasicDouble[@Descr = 'Input A'],//BaseNode/BasicDouble[@Descr = 'Input B'])"/>
<None attr1="0" attt2="1" attr2="0">
<xsl:apply-templates select="//BaseNode/BasicDouble[@Descr = 'Input A']"/>
<xsl:apply-templates select="//BaseNode/BasicDouble[@Descr = 'Input B']"/>
</None>
</xsl:copy>
</xsl:template>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.