1
votes

I want to do xml to xml tranformation using XSLT 1.0. I need to remove full node from XML, if "Removeornot" value is not "1".

I already tried to create template:

<xsl:template match="//Node[not(Grandchild0[not(Grand3child[not(Grand4child[not(Grand5child[not(Grand6child[not(Removeornot = 1)])])])])])]"/>

This template works fine if xml has less levels, but I cannot adobt it to my solution. So maybe somebody can help with it.

Source XML:

<?xml version="1.0" encoding="UTF-8"?>
<Family>
    <Node>
        <Child0>AAA</Child0>
        <Child1>BBB</Child1>
        <Child2>
            <Grandchild0>
                <Grand2child>DDD</Grand2child>
                <Grand3child>
                    <Grand4child>
                        <Grand5child>
                            <Grand6child>
                                <Removeornot>1</Removeornot>
                            </Grand6child>
                            <Grand6child1>QQQ </Grand6child1>
                        </Grand5child>
                        <Grand5child1>ZZZ </Grand5child1>
                    </Grand4child>
                </Grand3child>
                <Grand2child2>EEE</Grand2child2>
            </Grandchild0>
            <Grandchild2>FFF</Grandchild2>
            <Grandchild3>GGG</Grandchild3>
            <Grandchild4>HHH</Grandchild4>
            <Grandchild5>IIII</Grandchild5>
        </Child2>
    </Node>
    <Node>
        <Child0>AAA</Child0>
        <Child1>BBB</Child1>
        <Child2>
            <Grandchild0>
                <Grand2child>DDD</Grand2child>
                <Grand3child>
                    <Grand4child>
                        <Grand5child>
                            <Grand6child>
                                <Removeornot>YES</Removeornot>
                            </Grand6child>
                            <Grand6child1>QQQ </Grand6child1>
                        </Grand5child>
                        <Grand5child1>ZZZ </Grand5child1>
                    </Grand4child>
                </Grand3child>
                <Grand2child2>EEE</Grand2child2>
            </Grandchild0>
            <Grandchild2>FFF</Grandchild2>
            <Grandchild3>GGG</Grandchild3>
            <Grandchild4>HHH</Grandchild4>
            <Grandchild5>IIII</Grandchild5>
        </Child2>
    </Node>
    <Node>
        <Child0>AAA</Child0>
        <Child1>BBB</Child1>
        <Child2>
            <Grandchild0>
                <Grand2child>DDD</Grand2child>
                <Grand3child>
                    <Grand4child>
                        <Grand5child>
                            <Grand6child>
                                <Removeornot>NO</Removeornot>
                            </Grand6child>
                            <Grand6child1>QQQ </Grand6child1>
                        </Grand5child>
                        <Grand5child1>ZZZ </Grand5child1>
                    </Grand4child>
                </Grand3child>
                <Grand2child2>EEE</Grand2child2>
            </Grandchild0>
            <Grandchild2>FFF</Grandchild2>
            <Grandchild3>GGG</Grandchild3>
            <Grandchild4>HHH</Grandchild4>
            <Grandchild5>IIII</Grandchild5>
        </Child2>
    </Node>
</Family>

This XSLT just prints "Removeornot" value. Other approach to accomplish this task was to use this value as variable, but as I understand variables in XSLT is fully immutable and I cannot use them outside for-each loop. Any ideas how can I use this value to remove node if value content is wrong?

XSLT Source:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="v2">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>
<xsl:template match="/">
      <xsl:for-each select="Family/Node">
      <xsl:call-template name="tests"></xsl:call-template>
      <!-- Is it possible to get variable value here to understand if node shloud be deleted?
             <xsl:if test="$tester = 1">
             <xsl:call-template name="copyit"></xsl:call-template>
              </xsl:if> -->
       </xsl:for-each>
 </xsl:template>

<xsl:template name="tests">
<!--      <xsl:for-each select="Family/Node"> -->
    <xsl:for-each select="Child2/Grandchild0">
        <xsl:for-each select="Grand3child/Grand4child">
            <xsl:for-each select="Grand5child/Grand6child">
                <xsl:value-of select="Removeornot"></xsl:value-of>
                    <!--  <xsl:variable name="tester" select="Removeornot" ></xsl:variable>-->
                 </xsl:for-each>
             </xsl:for-each>
          </xsl:for-each>
 <!--      </xsl:for-each> -->
</xsl:template>

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

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

</xsl:stylesheet>

Expected result:

<?xml version="1.0" encoding="UTF-8"?>
<Family>
    <Node>
        <Child0>AAA</Child0>
        <Child1>BBB</Child1>
        <Child2>
            <Grandchild0>
                <Grand2child>DDD</Grand2child>
                <Grand3child>
                    <Grand4child>
                        <Grand5child>
                            <Grand6child>
                                <Removeornot>1</Removeornot>
                            </Grand6child>
                            <Grand6child1>QQQ </Grand6child1>
                        </Grand5child>
                        <Grand5child1>ZZZ </Grand5child1>
                    </Grand4child>
                </Grand3child>
                <Grand2child2>EEE</Grand2child2>
            </Grandchild0>
            <Grandchild2>FFF</Grandchild2>
            <Grandchild3>GGG</Grandchild3>
            <Grandchild4>HHH</Grandchild4>
            <Grandchild5>IIII</Grandchild5>
        </Child2>
    </Node>
</Family>

I'm new too XSLT, every help is appreciated.

1

1 Answers

1
votes

I need to remove full node from XML, if "Removeornot" value is not "1".

That would be

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" />

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

    <xsl:template match="Node[not(.//Removeornot = 1)]" />
</xsl:transform>
  1. The identity template copies every node unless a more specific template matches.
  2. The other template for Node[not(.//Removeornot = 1)] is more specific - and it outputs nothing for any nodes that match here, which in effect removes those nodes from the input.