need to use result of for each outside the loop

Here should go questions about transforming XML with XSLT and FOP.
Posts: 1
Joined: Fri May 24, 2024 1:08 am

need to use result of for each outside the loop

Post by joem »

I have a function which uses a for each to find the element desired and return a substring. I assign a variable from within the for-each loop, but I don't know how to make the scope more global, at least within this function. Once this function call returns the field, $lineAllocPct, the main part will use it in calculations.

Code: Select all

<xsl:function name="custom:getAllocPct">
		<xsl:param name="allocDetailStr"/>
		<xsl:param name="costCenter"/>
		<xsl:variable name="lineAllocPct"/>
		<xsl:for-each select="$allocDetailStr">
			<xsl:if test ="contains($allocDetailStr,$costCenter)">
				<xsl:variable name="lineAllocPct" select="normalize-space(substring-after($allocDetailStr,'having'))"/>
				<xsl:sequence select="$lineAllocPct"/>
	<xsl:sequence select="$lineAllocPct"/>

Thank you,
Martin Honnen
Posts: 101
Joined: Tue Aug 19, 2014 12:04 pm

Re: need to use result of for each outside the loop

Post by Martin Honnen »

XSLT's for-each is not really a loop.

As for your problem to assign the result of a for-each to a variable, you can do stuff like

Code: Select all

<xsl:variable name="result" as="item()*">
   <xsl:for-each select="$some-sequence">
      ... <xsl:sequence select="some-xpath-expression"/>
 <xsl:sequence select="$result"/>
On the other hand, it is often not necessary to use for-each/XSLT but you can just select the values with the power of pure XPath.

If you need more help consider to show the sample input data you want to process with your function, a sample function call and the result you expect your function to return. It would also help if you used `as` type declarations on your variables, currently you have parameter called `allocDetailStr`, it is not clear whether it will be a single string and why you then need to or want to use an iteration with for-each over a single string.
Post Reply