我如何选择一个标签并复制每个孩子期待的东西并给一个新的父母

时间:2016-09-29 09:53:58

标签: xml xslt

我需要选择一个带有所有子标签的标签,但我需要删除一个sepcial标签,可以在“main”标签中的任何位置。我的示例中的“main”标记是Deeper2,我需要删除所有“ShouldDelete”标记及其值(因此删除该行)。在我可以清除这个选定的标签之后,我需要把它放在一个新的“边框”中。我的xml是这样的:

<Edge>
    <Deeper>
        <Deeper2>
            <Something>..</Something>
            <Something2>..</Something2>
            <Something3>..</Something3>
            <Something4>..</Something4>
            <Something5>
                <Something6>..</Something6>
                <Something7>..</Something7>
                <ShouldDelet>..</ShouldDelet>
            </Something5>
            <Something8>..</Something8>
            <Something9>..</Something9>
            <Something10>
                <Something6>..</Something6>
                <ShouldDelet>..</ShouldDelet>
            </Something10>
            <ShouldDelet>..</ShouldDelet>
        </Deeper2>
    </Deeper>
</Edge>

我需要这个:

<Deeper2>
    <Something>..</Something>
    <Something2>..</Something2>
    <Something3>..</Something3>
    <Something4>..</Something4>
    <Something5>
        <Something6>..</Something6>
        <Something7>..</Something7>
    </Something5>
    <Something8>..</Something8>
    <Something9>..</Something9>
    <Something10>
        <Something6>..</Something6>
    </Something10>
</Deeper2>

毕竟需要把它放在一个像这样的新“边界”中:

<MyNewTag>
    <MyNewTag2>
        <Deeper2>
            <Something>..</Something>
            <Something2>..</Something2>
            <Something3>..</Something3>
            <Something4>..</Something4>
            <Something5>
                <Something6>..</Something6>
                <Something7>..</Something7>
            </Something5>
            <Something8>..</Something8>
            <Something9>..</Something9>
            <Something10>
                <Something6>..</Something6>
            </Something10>
        </Deeper2>
    </MyNewTag2>
</MyNewTag>

1 个答案:

答案 0 :(得分:0)

您需要一个身份模板(基本上是您输入的精确副本)并覆盖模板。以下样式表:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

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

    <xsl:template match="Edge">
        <MyNewTag>
            <xsl:apply-templates/>
        </MyNewTag>
    </xsl:template>

    <xsl:template match="Deeper">
        <MyNewTag2>
            <xsl:apply-templates/>
        </MyNewTag2>
    </xsl:template>

    <!-- an empty match deletes the node -->
    <xsl:template match="ShouldDelet"/>

</xsl:stylesheet>

当应用于您输入XML时,生成:

<?xml version="1.0" encoding="utf-8"?>
<MyNewTag>
   <MyNewTag2>
      <Deeper2>
         <Something>..</Something>
         <Something2>..</Something2>
         <Something3>..</Something3>
         <Something4>..</Something4>
         <Something5>
            <Something6>..</Something6>
            <Something7>..</Something7>
         </Something5>
         <Something8>..</Something8>
         <Something9>..</Something9>
         <Something10>
            <Something6>..</Something6>
         </Something10>
      </Deeper2>
   </MyNewTag2>
</MyNewTag>