XLST2 and stringmanipulation

Discussion in 'XML' started by Andreas Kraftl, Dec 18, 2004.

  1. Hello,

    i've a xml file:
    <lexikon>
    <begriff>
    <synonyme>
    <synonym>a</synonym>
    <synonym>b c</synonym>
    </synonym>
    <beschreibung>
    Great letters.
    </beschreibung>
    </begriff>
    ...
    </lexikon>

    Then there is an other XML file where i would every string from <synonym>
    extending.

    Example XML file:
    There is an b c and there an a

    should be extend to
    There is an
    <a href="lexikon.php" class="lexikon">b c<span>
    Great letters.</span></a>
    and there an <a href="lexikon.php" class="lexikon">a<span>
    Great letters.</span></a>

    Following works not:

    <!-- build the regex string -->
    <xsl:variable name="lexwordstring">
    <xsl:text>(.*?)(nurDummText</xsl:text>
    <xsl:for-each
    select="/pages/page/content/lexikon/begriff/synonyme/*">
    <xsl:text>|</xsl:text>
    <xsl:value-of select="normalize-space(.)"/>
    </xsl:for-each>
    <xsl:text>)([ ,.]*?)</xsl:text>
    </xsl:variable>

    <xsl:template match="text()">
    <xsl:analyze-string select="." regex="{$lexwordstring}">
    <xsl:matching-substring>
    <xsl:value-of select="regex-group(1)"/>
    <a href="{$lexikonpage}" class="lexikon">
    <xsl:value-of select="regex-group(2)"/>
    <span>
    <xsl:apply-templates select="..."/>
    </span>
    </a>
    </xsl:matching-substring>
    <xsl:non-matching-substring>
    <xsl:value-of select="."/>
    </xsl:non-matching-substring>
    </xsl:analyze-string>
    </xsl:template>

    Saxon means with <xsl:apply-templates>
    "Cannot select a node here: the context item is an atomic value"
    Without <xsl:apply-templates>, it works, but without the description ;).

    Every idea is welcome?

    Thanks
    Andy
     
    Andreas Kraftl, Dec 18, 2004
    #1
    1. Advertising

  2. Andreas Kraftl

    Joris Gillis Guest

    Hi,

    > <xsl:template match="text()">
    > <xsl:analyze-string select="." regex="{$lexwordstring}">
    > <xsl:matching-substring>
    > <xsl:value-of select="regex-group(1)"/>
    > <a href="{$lexikonpage}" class="lexikon">
    > <xsl:value-of select="regex-group(2)"/>
    > <span>
    > <xsl:apply-templates select="..."/>


    I've never touched XPath2.0 , but 3 dots in a row doesn't sound legal.
    You can use '..' to select the parent or '../..' to select the grandparent.

    > </span>
    > </a>
    > </xsl:matching-substring>
    > <xsl:non-matching-substring>
    > <xsl:value-of select="."/>
    > </xsl:non-matching-substring>
    > </xsl:analyze-string>
    > </xsl:template>


    regards,

    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Spread the wiki (http://en.wikipedia.org)
     
    Joris Gillis, Dec 18, 2004
    #2
    1. Advertising

  3. Andreas Kraftl wrote:


    > Following works not:
    >
    > <!-- build the regex string -->
    > <xsl:variable name="lexwordstring">
    > <xsl:text>(.*?)(nurDummText</xsl:text>
    > <xsl:for-each
    > select="/pages/page/content/lexikon/begriff/synonyme/*">
    > <xsl:text>|</xsl:text>
    > <xsl:value-of select="normalize-space(.)"/>
    > </xsl:for-each>
    > <xsl:text>)([ ,.]*?)</xsl:text>
    > </xsl:variable>
    >
    > <xsl:template match="text()">
    > <xsl:analyze-string select="." regex="{$lexwordstring}">
    > <xsl:matching-substring>
    > <xsl:value-of select="regex-group(1)"/>
    > <a href="{$lexikonpage}" class="lexikon">
    > <xsl:value-of select="regex-group(2)"/>
    > <span>
    > <xsl:apply-templates select="..."/>
    > </span>
    > </a>
    > </xsl:matching-substring>
    > <xsl:non-matching-substring>
    > <xsl:value-of select="."/>
    > </xsl:non-matching-substring>
    > </xsl:analyze-string>
    > </xsl:template>
    >
    > Saxon means with <xsl:apply-templates>
    > "Cannot select a node here: the context item is an atomic value"


    I don't really understand on which node you want to apply templates but
    it doesn't work as Saxon rightly tells you that the context item inside
    the matching-substring instruction is an atomic value, it is a string
    value resulting from the regular expression match.

    Using the following "dictionary"

    <?xml version="1.0" encoding="UTF-8"?>
    <lexikon>
    <begriff>
    <synonyme>
    <synonym>a</synonym>
    <synonym>b c</synonym>
    </synonyme>
    <beschreibung>
    Tolle Buchstaben.
    </beschreibung>
    </begriff>
    </lexikon>

    and the following XML input

    <?xml version="1.0" encoding="UTF-8"?>
    <text>Da ist ein b c und da ein a</text>

    the following XSLT 2.0 stylesheet

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

    <xsl:eek:utput method="xml" encoding="UTF-8" />

    <xsl:variable name="lexwordstring">
    <xsl:text>(</xsl:text>
    <xsl:for-each
    select="document('test2004121801.xml')/lexikon/begriff/synonyme/*">
    <xsl:value-of select="normalize-space(.)"/>
    <xsl:if test="position() != last()">
    <xsl:text>|</xsl:text>
    </xsl:if>
    </xsl:for-each>
    <xsl:text>)</xsl:text>
    </xsl:variable>

    <xsl:template match="text()">
    <xsl:analyze-string select="." regex="{$lexwordstring}">
    <xsl:matching-substring>
    <a href="" class="lexikon">
    <xsl:value-of select="regex-group(1)"/>
    <span>
    <xsl:value-of
    select="document('test2004121801.xml')/lexikon/begriff[synonyme/synonym
    = regex-group(1)]/beschreibung" />
    </span>
    </a>
    </xsl:matching-substring>
    <xsl:non-matching-substring>
    <xsl:value-of select="."/>
    </xsl:non-matching-substring>
    </xsl:analyze-string>
    </xsl:template>

    <xsl:template match="*">
    <xsl:copy>
    <xsl:apply-templates />
    </xsl:copy>
    </xsl:template>

    </xsl:stylesheet>

    outputs

    <?xml version="1.0" encoding="UTF-8"?><text>D<a href=""
    class="lexikon">a<span>
    Tolle Buchstaben.
    </span></a> ist ein <a href="" class="lexikon">b c<span>
    Tolle Buchstaben.
    </span></a> und d<a href="" class="lexikon">a<span>
    Tolle Buchstaben.
    </span></a> ein <a href="" class="lexikon">a<span>
    Tolle Buchstaben.
    </span></a></text>

    with Saxon 8.1.1, maybe that helps you to find a solution.
    I realize that more characters are matched than you are looking for
    (e.g. the 'a' in 'Da'), I have tried to remedy that with using \b in the
    regular expression but unfortunately it seems the regular expression
    syntax in XSLT 2.0/XPath 2.0 doesn't know \b (word boundary).


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 18, 2004
    #3
  4. Joris Gillis <> wrote:
    >> <xsl:apply-templates select="..."/>

    > I've never touched XPath2.0 , but 3 dots in a row doesn't sound legal.
    > You can use '..' to select the parent or '../..' to select the grandparent.


    Of course you are right.
    .... means just a placeholder. :)

    Thx
    Andy
    --
    Kraftl EDV - Dienstleistungen
    Linux, Schulungen, Webprogrammierung, Webdesign
    Telefon: +43(0)676/3533220
    E-mail: , Homepage: http://www.kraftl.at/
     
    Andreas Kraftl, Dec 19, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nut Cracker
    Replies:
    1
    Views:
    349
  2. jakk
    Replies:
    4
    Views:
    12,396
  3. Tim Meagher
    Replies:
    8
    Views:
    1,300
    Karl Seguin
    Jun 18, 2005
  4. Jerry Manner
    Replies:
    2
    Views:
    6,589
    SpaceGirl
    May 30, 2005
  5. titi

    if and and vs if and,and

    titi, Mar 9, 2007, in forum: VHDL
    Replies:
    4
    Views:
    602
    Mike Treseler
    Mar 11, 2007
Loading...

Share This Page