xsl-list
[Top] [All Lists]

Re: [xsl] XSL | index-of()

2019-09-27 04:29:40
index-of(A, B) tests whether the sequence A includes the value B, and if so, it 
returns the list of positions where it appears. If it doesn't appear anywhere, 
the returned list is empty.

The sequence ('ABG', 'AGI', 'BBL', 'ECK') doesn't include the value 'The 
abbreviation (ABG) appears in this doc.', so index-of() returns the emty 
sequence ().

contains(A, B) tests whether the string A contains B as a substring, and 
there's a special rule that if B is the empty sequence, then the function 
returns true (don't ask why...). In fact this is all wrong because index-of 
returns a sequence of integers, and the second argument of contains() requires 
an optional string, so at the very least you should get some kind of warning.

So that explains why your code isn't working. Now, how to fix it...

You only seem to be interested in knowing whether or not an abbreviation is 
present, and not in taking any particular action if it is (or in knowing which 
abbreviation is present. I think the simplest solution to that is:

<xsl:if test="some $a in $abbr satisfies contains($actdoc, concat('(', $a, 
')'))">

There are ways that could me made more efficient, e.g. by constructing a 
composite regular expression, but the above should do as a start.

Michael Kay
Saxonica

On 27 Sep 2019, at 10:08, Janine Lantzsch loderndesfeuer(_at_)gmx(_dot_)de 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Hello community,
 
I am new in this list and hope very much that someone can help me or give me 
an input with which I can continue to work.

I would like to use XSL to check whether a abbreviation from a list appears 
in a certain string of recurring elements. The abbreviation is always written 
in brackets. The source file looks like this (very simplified):
 
<?xml version="1.0" encoding="UTF-8"?>
<all>
    <abbreviations>
            <abbr>ABG</abbr>
            <abbr>AGI</abbr>
            <abbr>BBL</abbr>
            <abbr>ECK</abbr>
    </abbreviations>
    <documents>
        <doc no="1">The abbreviation (ABG) appears in this doc.</doc>
        <doc no="2">This doc has no shortcut.</doc>
        <doc no="3">An abbreviation (BBL).</doc>
        <doc no="4">And here (ECK).</doc>
        <doc no="5">And here again (ECK).</doc>
    </documents>
</all>

Only unfortunately I still have a bug somewhere with the index-of. Maybe the 
more experienced of you will see right away what's wrong with my code. I've 
been brooding since last night and can't find a solution :(
 
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
xmlns:fn="http://www.w3.org/2005/xpath-functions"; version="2.0">
    <xsl:output method="xml" encoding="UTF-8"/>
    <xsl:template match="all">
        <xsl:variable name="abbr" select="abbreviations/abbr"/>
        <all>
            <xsl:for-each select="//doc">
                <xsl:variable name="actdoc" select="."/>
                <xsl:if test="fn:contains($actdoc, fn:index-of($abbr, 
$actdoc))">
                    <doc>
                        <xsl:attribute name="no"><xsl:value-of 
select="@no"/></xsl:attribute>
                        <xsl:value-of select="$actdoc"/>
                    </doc>
                </xsl:if>
            </xsl:for-each>
        </all>
    </xsl:template>
</xsl:stylesheet>

The output should look something like this:
 
<all>
    <doc no="1">ABG</doc>
    <doc no="3">BBL</doc>
    <doc no="4">ECK</doc>
    <doc no="5">ECK</doc>
</all>

Thank you so much for your help!
 
Many greetings
Janine S.
XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by 
email <>)
--~----------------------------------------------------------------
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>