Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XLST2 and stringmanipulation

Reply
Thread Tools

XLST2 and stringmanipulation

 
 
Andreas Kraftl
Guest
Posts: n/a
 
      12-18-2004
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
 
Reply With Quote
 
 
 
 
Joris Gillis
Guest
Posts: n/a
 
      12-18-2004
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-v...i?userid=38041)
Spread the wiki (http://en.wikipedia.org)
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      12-18-2004


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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">

<xslutput 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/
 
Reply With Quote
 
Andreas Kraftl
Guest
Posts: n/a
 
      12-19-2004
Joris Gillis <(E-Mail Removed)> 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: http://www.velocityreviews.com/forums/(E-Mail Removed), Homepage: http://www.kraftl.at/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if and and vs if and,and titi VHDL 4 03-11-2007 05:23 AM



Advertisments