xsl-list
[Top] [All Lists]

Re: [xsl] Filtering on a tokenized list

2017-02-24 00:24:29
I played with your use case, and I think you need something like below:

XML document
<?xml version="1.0" encoding="UTF-8"?>
<X colwidth="0.439in 1.845in"/>

XSLT program
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                        version="2.0">

    <xsl:output method="text"/>

    <xsl:template match="X">
        <xsl:variable name="nums" select="tokenize(@colwidth,'\s+')"/>
        <xsl:for-each select="$nums">
            <xsl:value-of select="substring-before(., 'in')"/>
            <xsl:text>&#xa;</xsl:text>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

On 23 February 2017 at 18:19, Rick Quatro rick(_at_)rickquatro(_dot_)com <
xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Hi All,

I have an attribute value that I want to convert to a sequence of decimals:

@colwidth="0.439in 1.845in"

I am using tokenize to drop the measurement units before I convert each
member to decimal:

tokenize($widths,'[^\.\d]')

This works, except I get a empty string as the last item in the list. I saw
in Michael's book that I can filter out the empty member with a predicate:

tokenize($widths,'[^\.\d]')[.]

I can't quite understand how the predicate [.] works. Is it like saying
"non-empty strings"? Would it be equivalent to

[not(.="")]

Thank you in advance.

-Rick

By the way, here is my complete function. Any suggestions for improvements
are appreciated. I am going from absolute widths:

0.439in 1.845in

to a sequence of percentages:

19, 81

    <!-- Function for converting absolute table column widths to
proportional (percentage) widths. -->
    <xsl:function name="rq:getProportionalWidths">
        <!-- widths parameter is @colwidth in the form of absolute
dimensions. -->
        <xsl:param name="widths"/>
        <!-- Drop the measurement units and convert the strings to
decimals.
-->
        <xsl:variable name="tokens"
select="tokenize($widths,'[^\.\d]')[.]"/>
        <xsl:variable name="decimals" select="for $i in $tokens return
xs:decimal($i)"/>
        <!-- Divide each number by the total of all numbers and multiply by
100. -->
        <xsl:sequence select="for $i in $decimals return round($i div
sum($decimals) * 100)"/>
    </xsl:function>





-- 
with best regards,
Mukul gandhi
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>