XSLT: Use of template tag variable and break condition in for-each loop -
i using xslt have created <xsl:template>
tag validation using <xsl:for-each>
statement , sets value of <xsl:variable>
or <xsl:param>
true or false.
- is there way break statement in for-each if condition true?
- can use value of template variable or param main calling routine?
example:
<!-- main xslt --> <xsl:template> <xsl:call-template name ="testtemplate"> <!-- here want use variable or param defined in testtemplate, possible? --> </xsl:call-template> </xsl:template> <xsl:template name ="testtemplate"> <xsl:param name="eee"/> <xsl:for-each select ="//rootnode/leafnode"> <xsl:choose> <xsl:when test ="@type='abc'"> <xsl:value-of select ="true"/> </xsl:when> <xsl:otherwise>false</xsl:otherwise> </xsl:choose> </xsl:for-each> </xsl:template>
to questions:
is there way break statement in for-each if condition true ?
no, , unnecessary. xslt not imperative programming language, , imperative approaches not work here.
what seemingly want expressing "find first <leafnode>
@type='abc'
, , return true or false depending on whether there one.
the way in traditional languages approach: each node, check condition, if condition satisfied return.
in xslt select node xpath:
//rootnode/leafnode[@type='abc']
either result of contains node, or not. no need for-each @ all.
can use value of template variable or param main calling routine ?
no. variables , params strictly scoped. go out of scope once processing leaves parent element. constant, once declared cannot changed.
the way want here making template output desired value , capture in variable:
<xsl:template> <xsl:variable name="returnvalue"> <xsl:call-template name="testtemplate" /> </xsl:variable> </xsl:template> <xsl:template name="testtemplate"> <!-- following expression emits true or false --> <xsl:value-of select=" count(//rootnode/leafnode[@type='abc']) gt; 0 " /> </xsl:template>
two last hints:
- avoid
'//'
operator @ costs. of time use not necessary - the first, top-most element in document not "root node", "document element"
this important distinction. "root node" comes before document element, xpath above should more (semantically):
/documentelement/leafnode ^------ *this* slash represents "root node"
Comments
Post a Comment