Xslt 将XML子节点移动到父节点到XML的底部(当前父节点之外)

Xslt 将XML子节点移动到父节点到XML的底部(当前父节点之外),xslt,Xslt,如何将XML文件中的某些子节点与父节点以一对多的关系与其内容一起移动到父节点之外的XML文件底部。 首先是一个特定的节点,然后可能是两个不同的节点 在本例中,我希望将所有Child1及其内容移动到之外,并将其放置在下方和内 ---- ------ ------ ---- ------ ------ ---- ------ ------ 预期产量 <MyControls> <Parent id="1"> ---- <Child2

如何将XML文件中的某些子节点与父节点以一对多的关系与其内容一起移动到父节点之外的XML文件底部。 首先是一个特定的节点,然后可能是两个不同的节点

在本例中,我希望将所有Child1及其内容移动到
之外,并将其放置在
下方和


----
------
------
----
------
------
----
------
------
预期产量

<MyControls>
<Parent id="1">
    ----
             <Child2 id="1">
                 ------
             </Child2>
</Parent>
<Parent id="2">
    ----
             <Child2 id="11">
                 ------
             </Child2>
</Parent>
<Parent id="3">
    ----
             <Child2 id="111">
                 ------
             </Child2>
</Parent>

<Child1 id="1">
    ------
</Child1>

<Child1 id="11">
  ------
</Child1>
<Child1 id="111">
   ------
</Child1>
</MyControls>

----
------
----
------
----
------
------
------
------
使用XSLT

<xsl:template match="@*|node()">
<xsl:copy>
  <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
</xsl:template>      
 <xsl:template match="Child1">
 <Child1>     
    <xsl:apply-templates select="@*[not(self::Child1)]" />
    <xsl:apply-templates select="node()[not(self::Child1)]" />
 </Child1>    
</xsl:template>

您的样式表只是一个开始,但是您需要更多的异常来防止
子元素1
被复制到
父元素中

试着这样做:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Parent">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="node()[not(self::Child1)]"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Controls">
    <xsl:copy>
      <xsl:apply-templates select="Parent"/>
      <xsl:apply-templates select="Parent/Child1"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
基于注释中请求的新XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Controls">
        <MyControls>
            <xsl:apply-templates select="@*|node()"/>
            <xsl:apply-templates select="Parent/Child1"/>
        </MyControls>
    </xsl:template>

    <xsl:template match="Parent">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()[not(self::Child1)]"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="@ID">
        <xsl:attribute name="id">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>

    <xsl:template match="@*[not(name()='ID')]"/>

</xsl:stylesheet>


在这种情况下,您是否可以显示希望输出的XML?此外,如果您已经尝试过任何XSLT,那么如果您也展示了它,将会有所帮助。谢谢添加了预期的输出和使用的XSLT。我理解。但是如果Child1可以来自任何级别的child呢。例如,如果Child1也是Child2的孩子。我们如何更改这一行以从任何级别获取Child1,而不仅仅是从子级到父级。您描述的内容将与查找任何后代的
select=“Parent//Child1”
相匹配,但我怀疑您还会有更多的异常。实际上,如果我只是将Parent/Child1更改为//Child1,它将正常工作。。是的,如果
Child1
可以在任何地方,那也行。您的示例XML没有提供足够的信息,无法知道在何处可以使用这些元素。@DanielHaley看起来不错。似乎OP不想尝试理解XSL/XPath哈哈
<Controls>
  <Parent id="1">
    ----

             <Child2 id="1">
                 ------
             </Child2>
</Parent>
  <Parent id="2">
    ----

             <Child2 id="11">
                 ------
             </Child2>
</Parent>
  <Parent id="3">
    ----

             <Child2 id="111">
                 ------
             </Child2>
</Parent>
  <Child1 id="1">
                 ------
             </Child1>
  <Child1 id="11">
                 ------
             </Child1>
  <Child1 id="111">
                 ------
             </Child1>
</Controls>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Controls">
        <MyControls>
            <xsl:apply-templates select="@*|node()"/>
            <xsl:apply-templates select="Parent/Child1"/>
        </MyControls>
    </xsl:template>

    <xsl:template match="Parent">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()[not(self::Child1)]"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="@ID">
        <xsl:attribute name="id">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>

    <xsl:template match="@*[not(name()='ID')]"/>

</xsl:stylesheet>