Search code examples
xmlxsltxpathpredicates

XSLT: Difference between using predicates and <xsl:if> statement


I am trying to understand if there is any difference between using predicates and using an <xsl:if> ?

From a performance perspective, is the use of a predicate any better? Does predicates also traverse through each node in the xml tree to identify the nodes based on the filter criteria?


Solution

  • The decision between placing conditionals within XPath predicates or xsl:if (or xsl:when) tests is really one of style, not performance.

    Procedural code can be written in XSLT using loops and if statements, but to leverage the power of XSLT elegantly, base your XSLT upon its declarative pattern matching and transformation capabilities.

    Instead of thinking procedurally (do this; then do that), think about how to declaratively express relationship between input XML and output XML via matching and transforming (match input XML and map to output XML This is the purpose of xs:template.). Predicates help you express pattern matching declaratively and therefore are preferred over procedural xsl:if statements when using XSLT the way it was designed to be used.