Stephen
Stephen

Reputation: 65

How to do an 'and' statement in XSLT select?

What is an example of a compound select statement using an AND operator, similar to an if statement where condition 1 = true and condition 2 = true?

Upvotes: 5

Views: 15650

Answers (3)

user227466
user227466

Reputation:

Here is an example of how to use a compound select statement....

<?xml version="1.0" encoding="ISO-8859-1"?>
<A>
    <B>
        <C>1</C>
        <D>2</D>
    </B>
    <B>
        <C>1</C>
        <D>3</D>
     </B>
    <E>test</E>
</A>

and your current template match is for "E", then try the below code to select only B where C = 1 and D = 3: for C and D read condition 1 = true and condition 2 = true

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" indent="yes"/>
    <xsl:template match="E">
        <xsl:value-of select="../B[C = 1][D = 3]"></xsl:value-of>
    </xsl:template>
    <xsl:template match="C"/>
    <xsl:template match="D"/>
</xsl:stylesheet>

Good Luck

Upvotes: 3

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

Here is one of the simplest possible examples:

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

 <xsl:template match=
    "num[. mod 2  = 0 and . mod 3 = 0]">
  <xsl:copy-of select="."/>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>

when this transformation is applied on this XML document:

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>

the wanted, correct result is produced:

<num>06</num>

Upvotes: 4

ed209
ed209

Reputation: 238

Use the AND operator:

<xsl:if test="a and b">

Upvotes: 1

Related Questions