multix
multix

Reputation: 15

XSLT: test condition on two nodes

I need to create an additional node, if the following condition is true:

IF NOT (TYPE = 'WE' AND 'CH' = 'true') -> create new node MISSING

Source:

<?xml version="1.0" encoding="UTF-8"?>
<Partner>
    <NR>10</NR>
    <SR>
        <PF>
            <TYPE>WE</TYPE>
            <NR>2345</NR>
            <CH>false</CH>
        </PF>
        <PF>
            <TYPE>WE</TYPE>
            <NR>111</NR>
            <CH>false</CH>
        </PF>
        <PF>
            <TYPE>RG</TYPE>
            <NR>999</NR>
            <CH>true</CH>
        </PF>
    </SR>
    <SR>
    ...
    </SR>
</Partner>

Desired Output:

<?xml version="1.0" encoding="UTF-8"?>
<Partner>
    <NR>10</NR>
    <SR>
        <PF>
            <TYPE>WE</TYPE>
            <NR>2345</NR>
            <CH>false</CH>
        </PF>
        <PF>
            <TYPE>WE</TYPE>
            <NR>111</NR>
            <CH>false</CH>
        </PF>
        <PF>
            <TYPE>RG</TYPE>
            <NR>999</NR>
            <CH>true</CH>
        </PF>
        <XX>MISSING</XX>
    </SR>
    <SR>
    ...
    </SR>
</Partner>

I really struggle to create this test condition - I need to check the content for two nodes:

<xsl:if test="not(./PF/TYPE = 'WE' and CH = 'true')">

I need to ensure that <CH> is evaluated in the <TYPE> = 'WE' context, but I do not how to do this...

My xslt looks like this:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

<xsl:template match="Partner">
<PARTNER>

    <xsl:for-each select="./SR">
           <SR>
           ...

           <xsl:copy-of select= "./PF">

           <xsl:if test="not(./PF/TYPE = 'WE' and CH = 'true')">
             <XX>MISSING</XX>
           </xsl:if>

           </SR>
    </xsl:for-each>

</PARTNER>
</xsl:template>
</xsl:stylesheet>

Upvotes: 1

Views: 493

Answers (1)

AJNeufeld
AJNeufeld

Reputation: 8705

You need to find a PF node which satisfies your condition, not a complex expression which can be satisfied by multiple nodes.

<xsl:if test="not(./PF[TYPE = 'WE' and CH = 'true'])">
    <XX>MISSING</XX>
</xsl:if>

Here, the expression ./PF[cond] finds a PF node which satisfies the condition cond. The required condition TYPE = 'WE' and CH = 'true' is evaluated in the context of that node.

Upvotes: 1

Related Questions