pravz
pravz

Reputation: 235

how to use two conditions in select conditions in xslt when using Apply template

<xsl:apply-templates mode="block2sequence" select="NewDataSet/Table[CTD_CTD_PKG_ID =$PackageId][position()=1] and NewDataSet/Table[CTD_SEQ_NUM =$strXSLMsgType][position()=1]"/>

why cant i use two conditions in above select condition, can any one suggest me

Upvotes: 0

Views: 15856

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

<xsl:apply-templates mode="block2" 
     select="NewDataSet/Table[CTD_CTD_PKG_ID =$PackageId][position()=1] "/>

why cant i use two conditions in above select condition

I guess this is to mean, "why can't the two conditions be specified in the same predicate?"

The answer is that the expression:

NewDataSet/Table[CTD_CTD_PKG_ID =$PackageId and position() = 1]

isn't equivalent at all to the 1st expression above.

The first expression selects the first Table child of NewDataSet such that the string value of its CTD_CTD_PKG_ID child is equal to the string value of $PackageId. In this case we don't know which child (at which position) of NewDataSet will be selected -- any child that happens to be the first with the specified properties, will be selected.

On the other side, the latter expression selects the first Table child of NewDataSet only if the string value of its CTD_CTD_PKG_ID child is equal to the string value of $PackageId. In this case, if anything is selected, it would be the first Table child.

If you want an equivalent expression to the first one, that has only one predicate, one such expression is:

NewDataSet/Table
      [CTD_CTD_PKG_ID =$PackageId 
     and 
       not(preceding-sibling::Table[CTD_CTD_PKG_ID =$PackageId ])
      ]

Update: The OP has published a code snippet:

 <xsl:apply-templates mode="block2sequence" select=
    "NewDataSet/Table[CTD_CTD_PKG_ID =$PackageId][position()=1]
    and
     NewDataSet/Table[CTD_SEQ_NUM =$strXSLMsgType][position()=1]"/>

This code will cause an error thrown at compile time by the XSLT processor.

The value of the select attribute is a boolean (expr1 and expr2), however templates in XSLT 1.0 and XSLT 2.0 can only be applied on nodes. A boolean isn't a node -- hence the error.

Solution:

My first guess is that you want templates to be applied on both nodes. If this is so, then use:

 <xsl:apply-templates mode="block2sequence" select=
    "NewDataSet/Table[CTD_CTD_PKG_ID =$PackageId][1]
    |
     NewDataSet/Table[CTD_SEQ_NUM =$strXSLMsgType][1]"/>

My second guess is that you want templates applied only on the first of the two nodes. If this is so, then use:

 <xsl:apply-templates mode="block2sequence" select=
    "(NewDataSet/Table[CTD_CTD_PKG_ID =$PackageId]
    |
     NewDataSet/Table[CTD_SEQ_NUM =$strXSLMsgType]
     )
      [1]
"/>

Notes:

  1. Please, learn how to ask a question -- provide all relevant data and explain -- in the question, not in subsequent comments.

  2. Did you know that [1] is equivalent to [position()=1] and is shorter?

Upvotes: 4

Michael Kay
Michael Kay

Reputation: 163458

You can use two conditions and your expression looks perfectly correct. If it is failing with an error, please tell us the error. If it is not selecting what you want, then (a) show us your source document, and (b) tell us what you want to be selected.

(You know, your question gives so little information, you don't give the impression that you really want an answer.)

Upvotes: 1

Related Questions