Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XSLT Element

XSLT xsl:if Element


xsl:if The element is used to perform conditional judgment on XML data, outputting the corresponding content only when the specified condition is met.


xsl:if Element

In XSLT, if you need to decide whether to output a piece of content based on the value or state of an XML node, you can use the xsl:if element for conditional control.

Syntax

  ... content to execute when the condition is met ...

**Note:** The test attribute is used to specify a conditional expression (an XPath expression). Only when the expression evaluates to true is the inner content processed and output.


Where to place the xsl:if Element

Typically,xsl:if It is used together with loop statements, for example in xsl:for-each to filter each node:

Example

<

xsl:stylesheet

version

=

"

1.0

"

xmlns:xsl

=

"

http://www.w3.org/1999/XSL/Transform

"

<

xsl:template

match

=

"

/

"

<

html

<

body

<

h2

My CD Collection

h2

<

table

border

=

"

1

"

<

tr

bgcolor

=

"

#9acd32

"

<

th

Title

th

<

th

Artist

th

<

th

Price

th

tr

<

xsl:for-each

select

=

"

catalog/cd

"

<

xsl:if

test

=

"

price

>

10

"

<

tr

<

td

<

xsl:value-of

select

=

"

title

"

/>

td

<

td

<

xsl:value-of

select

=

"

artist

"

/>

td

<

td

<

xsl:value-of

select

=

"

price

"

/>

td

tr

xsl:if

xsl:for-each

table

body

html

xsl:template

xsl:stylesheet

Try it »

**Note:** The test attribute is required, and its value is an XPath expression, such as a comparison operation (>,<, =) or a logical judgment.

**Note:** In the example above, the corresponding CD is output to the table only when price is greater than 10.