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

Subject: Re: [xsl] XSL | index-of()
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 27 Sep 2019 09:30:42 -0000
Am 27.09.2019 um 11:14 schrieb Janine Lantzsch loderndesfeuer@xxxxxx:
> 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>
> B B  B <abbreviations>
> B B  B B B  B B B  B <abbr>ABG</abbr>
> B B  B B B  B B B  B <abbr>AGI</abbr>
> B B  B B B  B B B  B <abbr>BBL</abbr>
> B B  B B B  B B B  B <abbr>ECK</abbr>
> B B  B </abbreviations>
> B B  B <documents>
> B B  B B B  B <doc no="1">The abbreviation (ABG) appears in this doc.</doc>
> B B  B B B  B <doc no="2">This doc has no shortcut.</doc>
> B B  B B B  B <doc no="3">An abbreviation (BBL).</doc>
> B B  B B B  B <doc no="4">And here (ECK).</doc>
> B B  B B B  B <doc no="5">And here again (ECK).</doc>
> B B  B </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">
> B B  B <xsl:output method="xml" encoding="UTF-8"/>
> B B  B <xsl:template match="all">
> B B  B B B  B <xsl:variable name="abbr" select="abbreviations/abbr"/>
> B B  B B B  B <all>
> B B  B B B  B B B  B <xsl:for-each select="//doc">
> B B  B B B  B B B  B B B  B <xsl:variable name="actdoc" select="."/>
> B B  B B B  B B B  B B B  B <xsl:if test="fn:contains($actdoc,
fn:index-of($abbr,
> $actdoc))">


I don't think you want to use `index-of` for the check, nor do you need
to use a nested variable and the if, you can write a predicate

 B B B  <xsl:for-each select="//doc[some $a in $abbr satisifies contains(.,
$a)]">

> B B  B B B  B B B  B B B  B B B  B <doc>
> B B  B B B  B B B  B B B  B B B  B B B  B <xsl:attribute
name="no"><xsl:value-of
> select="@no"/></xsl:attribute>
> B B  B B B  B B B  B B B  B B B  B B B  B <xsl:value-of select="$actdoc"/>

If you want to output the found abbreviation then use

 B B B B B B B B B B B B B B B B B B B B B B B B B B B  <xsl:value-of
select="$abbr[contains(current(), .)]"/>

Current Thread