0

From the following XML file, I want to remove "ColumnBlocks" tag based on one condition. Condition is- If the value of the FixedFloat tag is Float, then I want to delete ColumnBlocks tag from XML. My XML is -

<?xml version="1.0" encoding="UTF-8"?>
<FirstTag>
    <SecondTag>
        <CategoryGroups>
            <Group>
                <Category>
                    <PricingDetails>
                        <SimplePricingDetails>
                            <FixedFloat>Fixed</FixedFloat>
                        </SimplePricingDetails>
                    </PricingDetails>
                    <rowVolume>
                        <ColumnBlocks>
                            <ColumnBlock>
                                <Column>abcd</Column>                               
                            </ColumnBlock>
                            <ColumnBlock>
                                <Column>xyz</Column>                                
                            </ColumnBlock>
                        </ColumnBlocks>
                    </rowVolume>
                    <row>jsdf<row>
                </Category>
                <Category>
                    <PricingDetails>
                        <SimplePricingDetails>
                            <FixedFloat>Float</FixedFloat>
                        </SimplePricingDetails>
                    </PricingDetails>
                    <rowVolume>
                        <ColumnBlocks>
                            <ColumnBlock>
                                <Column>abcd</Column>                               
                            </ColumnBlock>
                            <ColumnBlock>
                                <Column>xyz</Column>                                
                            </ColumnBlock>
                        </ColumnBlocks>
                    </rowVolume>
                    <row>jsdf<row>
                </Category>
            </Group>
        </CategoryGroups>
    </SecondTag>
</FirstTag>

I want the final code to not have ColumnBlocks from the Category, where FixedFloat value is Float. It should look like the below XML -

<?xml version="1.0" encoding="UTF-8"?>
<FirstTag>
    <SecondTag>
        <CategoryGroups>
            <Group>
                <Category>
                    <PricingDetails>
                        <SimplePricingDetails>
                            <FixedFloat>Fixed</FixedFloat>
                        </SimplePricingDetails>
                    </PricingDetails>
                    <rowVolume>
                        <ColumnBlocks>
                            <ColumnBlock>
                                <Column>abcd</Column>                               
                            </ColumnBlock>
                            <ColumnBlock>
                                <Column>xyz</Column>                                
                            </ColumnBlock>
                        </ColumnBlocks>
                    </rowVolume>
                    <row>jsdf<row>
                </Category>
                <Category>
                    <PricingDetails>
                        <SimplePricingDetails>
                            <FixedFloat>Float</FixedFloat>
                        </SimplePricingDetails>
                    </PricingDetails>
                    <rowVolume>
                    </rowVolume>
                    <row>jsdf<row>
                </Category>
            </Group>
        </CategoryGroups>
    </SecondTag>
</FirstTag>

2
  • I do not understand why would you delete it. It is never good practice to delete something from generated xml file. You can simply check if that's <FixedFloat>Float</FixedFloat> and then ignore that node Commented Mar 11, 2020 at 15:00
  • I am sorry if I misunderstood but I want to delete from original XML not generated one. I want to remove that node from the final XML as I don't need that node if <FixedFloat> is equal to Float. Commented Mar 11, 2020 at 15:20

3 Answers 3

1

Use the identity transformation template as a starting point and then add an empty template for the elements you want to remove:

<?xml version="1.0" encoding="UTF-8"?>
<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="Category[PricingDetails/SimplePricingDetails/FixedFloat = 'Float']/rowVolume/ColumnBlocks"/>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/93dFepx

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect, it worked! Thanks a lot. Since I am new to this, would you mind explaining how the above XSLT works?
@Kartik, if "use the identity transformation template as a starting point and then add an empty template for the elements you want to remove" doesn't sound like an explanation to you then I guess you need to start with a book, cranesoftwrights.github.io/books/ptux/index.htm is one serving as an introduction and is available online.
0

You can try this:

<?xml version="1.0" encoding="UTF-8"?>
<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="ColumnBlocks">
        <xsl:choose>
            <xsl:when test="ancestor::Category/descendant::FixedFloat = 'Float'"> </xsl:when>
            <xsl:otherwise>
                <text>NO Output</text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
</xsl:stylesheet>

Comments

0

You Can also try smallest code which following:

<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="//ColumnBlocks[ancestor::Category/descendant::FixedFloat = 'Float']"/>
</xsl:stylesheet>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.