needxslthelp
needxslthelp

Reputation: 25

XSLT conditionally remove nodes

I have XML like this:

<?xml version="1.0" encoding="UTF-8"?>
<Info>
    <Type>A</Type>
    <Cycle>
        <Code>A</Code>
        <CycleNumber>63-1</CycleNumber>
        <CycleDate>2014-10-15T00:00:00</CycleDate>
        <CycleNetAmount>2400.00</CycleNetAmount>
        <CycleTaxAmount>432.00</CycleTaxAmount>
        <CycleTotalAmount>2832.00</CycleTotalAmount>
        <CyclePayableAmountAsText/>
    </Cycle>
    <Cycle>
        <Code>B</Code>
        <CycleNumber>63-1</CycleNumber>
        <CycleDate>2014-10-15T00:00:00</CycleDate>
        <CycleNetAmount>400.00</CycleNetAmount>
        <CycleTaxAmount>72.00</CycleTaxAmount>
        <CycleTotalAmount>472.00</CycleTotalAmount>
        <CyclePayableAmountAsText/>
    </Cycle>
</Info>

If Info/Type = A I would like to remove the Cycle blocks where Code = B. If Info/Type = B I would like to remove the Cycle blocks where Code = A.

Can someone please help?

Upvotes: 2

Views: 1115

Answers (1)

keshlam
keshlam

Reputation: 8058

To "remove nodes" in XSLT, you want to copy everything except those nodes. So: Start with the identity stylesheet (as usual), then add templates which match the nodes you want to exclude but which produce no output.

In this case the elements you want to discard can be described as "Any Cycle such that the value of its Code child element matches that of the /Info/Type element", which translates directly to

match="Cycle[Code=/Info/Type]"

There are other ways to express it, as always, with varying efficiency... but this is probably the simplest solution.

Upvotes: 1

Related Questions