CodeLearner
CodeLearner

Reputation: 237

XSLT Error : sequence of more than one item is not allowed as the value of variable

I have the following xml

<EVT EvtLevel="1">
    <EVT_EX>
        <FIELD1></FIELD1>
    </EVT_EX>
    <SESNUM>10</SESNUM>
    <WORK_ID>WO-001</WORK_ID>
</EVT>

I want to check if any node is present under EVT parent node other than EVT_EX and SESNUM and assign true/false to a variable. Say the variable is filterFlag

If I perform the check by iterating on each node under EVT using for-each then I get the following error

A sequence of more than one item is not allowed as the value of variable $filterFlag (false, false, ...)

As per the above xml filterFlag should have a true value.

For the xml given below the value should be false.

<EVT EvtLevel="1">
    <EVT_EX>
        <FIELD1></FIELD1>
    </EVT_EX>
    <SESNUM>10</SESNUM>
</EVT>

Please help

Upvotes: 1

Views: 1246

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

In XPath 1.0:

*[not(self::EVT_EX or self::SESNUM)] and 1

XSLT 1.0 - based verification:

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

  <xsl:template match="EVT">
    <xsl:value-of select="*[not(self::EVT_EX or self::SESNUM)] and 1"/>
  </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document (listed below):

<EVT EvtLevel="1">
    <EVT_EX>
        <FIELD1></FIELD1>
    </EVT_EX>
    <SESNUM>10</SESNUM>
</EVT>

the XPath expression is evaluated and the (correct/wanted) result of this evaluation is output:

false

When applied on the other provided XML document:

<EVT EvtLevel="1">
    <EVT_EX>
        <FIELD1></FIELD1>
    </EVT_EX>
    <SESNUM>10</SESNUM>
    <WORK_ID>WO-001</WORK_ID>
</EVT>

again the wanted, correct result is produced:

true

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167706

If you don't show your code at all we can't tell where the error is but if you want a single boolean value then you need to write an expression that evaluates to a single boolean value and not one that returns a sequence of several boolean values so use e.g. <xsl:variable name="others" as="xs:boolean" select="exists(* except (EVT_EX, SESNUM))"/>.

Upvotes: 0

Related Questions