SeeDerekEngineer
SeeDerekEngineer

Reputation: 780

XSLT if statement on element with multiple attributes

I have the following XML code:

<MainElement name = "apple">
   <Parameter variable1 = "foo" variable2 = "bar"/>
   <Parameter variable1 = "foo" variable2 = "notBar"/>

I am using xsl to print out the name of the MainElement if it contains a parameter with both variable1 == "foo" and variable2 == "bar", as follows:

<xsl:for-each select = "MainElement">
   <xsl:if test = "Parameter/@variable1 = 'foo' and Parameter/@variable2 = 'bar'">
      ...
   </xsl:if>
   <xsl:if test = "Parameter/@variable1 = 'foo' and Parameter/@variable2 = 'notBar'">
</xsl:for-each>

The problem is that this xsl code doesn't examine if one Parameter element contains both appropriate attributes, it looks at both Parameter elements to see if either contains the appropriate attributes.

I know I could try another method without the if's, but now for my own curiosity I want to know how can I have the if statement look for the attribute conditionals in one specific element, not among both?

Upvotes: 1

Views: 1608

Answers (1)

kjhughes
kjhughes

Reputation: 111491

Place the tests within a single predicate test on Parameter.

For example, change

Parameter/@variable1 = 'foo' and Parameter/@variable2 = 'bar'

to

Parameter[@variable1 = 'foo' and @variable2 = 'bar']

so that the test concerns a single Parameter child of MainElement that has both targeted attribute values, rather than possibly separate Parameter elements that could separately qualify.

Upvotes: 3

Related Questions