Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT: how to check numerous nodes for a certain value applied to all of them

I have the following simplified XML source:

<?xml version="1.0" encoding="UTF-8"?>
<MVKE>
<item>
    <MANDT>025</MANDT>
    <MATNR>000000000000000551</MATNR>
    <VMSTA>2</VMSTA>
</item>
<item>
    <MANDT>025</MANDT>
    <MATNR>000000000000000551</MATNR>
    <VMSTA>2</VMSTA>
</item>
<item>
    <MANDT>025</MANDT>
    <MATNR>000000000000000551</MATNR>   
    <VMSTA>2</VMSTA>
</item>
</MVKE>

and I need to compare the <VMSTA> values. If they are all "2" then my flag-value in the target XML should say "true" otherwise "false".

I have come up with this XSLT:

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

<xsl:template match="/">
    <list>
        <xsl:apply-templates select="MVKE"/>
    </list>
</xsl:template>

<xsl:template match="MVKE">
    <flag>
        <xsl:for-each select="item">
            <xsl:choose>
                <xsl:when
                    test="(preceding-sibling::*[1]/VMSTA or self::*/VMSTA = current()/VMSTA) and (current()/VMSTA='2')">
                    <xsl:value-of select="'true'"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="'false'"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </flag>
</xsl:template>

</xsl:stylesheet>

but the output is always

<?xml version="1.0" encoding="UTF-8"?>
<list>
<flag>truetruetrue</flag>
</list>

because of the <xsl:for-each>. I have also tried it with keys on <VMSTA> but that also gave me 3 values in <flag>. How can I compare the 3 properly and only get one value in <flag>? Am I just thinking to complicated?

like image 846
Peter Avatar asked Dec 22 '22 03:12

Peter


2 Answers

You don't need a for-each. Assuming the context node is an MVKE element (as in your example), the following expression returns true when all VMSTA elements have a string value equal to 2 (and false when any other value is present):

not(item/VMSTA[not(.='2')])

This works because of the implicit conversion of item/VMSTA[not(.='2')] into a boolean. A node-set's boolean value is true if and only if it is non-empty. Therefore, item/VMSTA[not(.='2')] is true whenever it selects at least one node, which is when there exists a VMSTA element whose string value is not 2. Wrapping the expression in not() produces its negation, which is the desired result.

Complete example:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes" method="xml" encoding="UTF-8"/>
    <xsl:template match="/">
        <list><xsl:apply-templates select="MVKE"/></list>
    </xsl:template>
    <xsl:template match="MVKE">
        <flag><xsl:value-of select="not(item/VMSTA[not(.='2')])"/></flag>
    </xsl:template>
</xsl:stylesheet>
like image 156
Wayne Avatar answered Jan 01 '23 09:01

Wayne


Just use:

not(/*/*/VMSTA[not(. = '2')])

This XPath expression evaluates to false() if there exists an element /*/*/VMSTA, whose string value is different from the string "2".

Otherwise true().

This may be referred to as the "law of double negation`

like image 42
Dimitre Novatchev Avatar answered Jan 01 '23 10:01

Dimitre Novatchev