[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

How to convert a recursive function to a loop, using X

Subject: How to convert a recursive function to a loop, using XSLT 2.0?
From: "Costello, Roger L. costello@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 10 May 2019 11:20:49 -0000
 How to convert a recursive function to a loop
Hello XSLT experts!

My input file consists of a sequence (sourceSeq) of <Byte> elements
representing a sequence of null-terminated strings. I want to create a
function that returns a sequence of <String> elements. For example, with this
sourceSeq:

<Byte>48</Byte>
<Byte>69</Byte>
<Byte>00</Byte>
<Byte>4A</Byte>
<Byte>69</Byte>
<Byte>6C</Byte>
<Byte>6C</Byte>
<Byte>00</Byte>

the function should return:

<String>Hi</String>
<String>Jill</String>

The strings are of variable length.

I do not know how many null-terminated strings are in sourceSeq. However, I do
know the total number (total-size) of <Byte> elements within sourceSeq
containing the null-terminated strings.

Below is a recursive way to implement the function. Unfortunately, total-size
can be quite large, which means the function recurses many times, resulting in
a "Too many nested function calls" error. Is there an iterative way to
implement the function?  /Roger

<xsl:function name="f:make-string-table-entries" as="element(String)*">
    <xsl:param name="total-size" as="xs:integer" />
    <xsl:param name="current-size" as="xs:integer" />
    <xsl:param name="current-position" as="xs:integer" />
    <xsl:param name="sourceSeq" as="element(Byte)*" />

    <xsl:choose>
        <xsl:when test="$current-size ge $total-size" />
        <xsl:otherwise>
            <xsl:variable name="string"
select="f:make-element-from-null-terminated-string('String',
$current-position, $sourceSeq)" as="element(String)"/>
            <xsl:sequence select="$string" />
            <xsl:variable name="length" select="string-length($string/text())
+ 1"/>  <!-- add 1 for the null byte -->
            <xsl:sequence select="f:make-string-table-entries($total-size,
xs:integer($current-size+$length), xs:integer($current-position+$length),
$sourceSeq)" />
        </xsl:otherwise>
    </xsl:choose>

</xsl:function>

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.