Reputation: 1796
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?
Upvotes: 5
Views: 3953
Reputation: 11223
I recommend using a key to get a group of nodes with different values for <VMSTA/>
. If the group only has one node, then all the values are identical:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:key name="dupes" match="VMSTA" use="."/>
<xsl:template match="/">
<xsl:variable name="vmsta-count"
select="count(//VMSTA[generate-id() =
generate-id(key('dupes', .))])"/>
<list>
<flag>
<xsl:choose>
<xsl:when test="$vmsta-count = 1">
<xsl:text>true</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>false</xsl:text>
</xsl:otherwise>
</xsl:choose>
</flag>
</list>
</xsl:template>
</xsl:stylesheet>
When run against your provided source results in:
<?xml version="1.0"?>
<list>
<flag>true</flag>
</list>
But, this is a general-case solution, and doesn't care about the actual value, just differences in value.
I hope this helps.
Upvotes: 1
Reputation: 60414
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>
Upvotes: 9
Reputation: 243529
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`
Upvotes: 3