XSLT 2.0 - 将现有子节点移动到新创建的节点

时间:2018-01-31 12:08:38

标签: xml xslt xslt-2.0

<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>

上面是我尝试用于将现有子节点移动到新创建的节点的XSLT。 该规则是创建新节点并将节点复制为子节点。 问题是我仍然在父节点中有旧元素。 我想要的是剪切和粘贴操作,而不是复制粘贴。 预先感谢您的帮助。

输入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>

1 个答案:

答案 0 :(得分:1)

请提供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>