This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: mixed content searching
- From: "Michael Kay" <michael dot h dot kay at ntlworld dot com>
- To: <xsl-list at lists dot mulberrytech dot com>
- Date: Tue, 7 May 2002 09:18:21 +0100
- Subject: RE: [xsl] mixed content searching
- Reply-to: xsl-list at lists dot mulberrytech dot com
> I just subscribed a few minutes ago.
This is a bit like the vicar complaining in his sermon about the people who
aren't in church, but I do wish people would lurk for a week or too before
posting.
I'd like to get some help on or
> probably some sample code that can do the following for me.
> The given XML file has mixed contents data such as
>
> <doc>hello <name>Tom</name>. i am <name>Sue</name>. cheers</doc>
>
> Q1) how do I write grep.xsl to meet the following commands
> H:> saxon example.xml grep.xsl target=Tom
> Tom
>
> H:> saxon example.xml grep.xsl target=am
> .... i am ...
>
> H:> saxon example.xml grep.xsl target=he
> hello ...
> .... cheers
Guess: you want to output the contents of the text nodes containing the
parameter string. If the node has following-sibling text nodes, follow it by
"...". If it has preceding-sibling text nodes, precede it by "...". Output
each match on a new line, with output method = text.
Solution:
<xsl:template match="text()">
<xsl:if test="contains(., $target)">
<xsl:if test="preceding-sibling::text()">... </xsl:if>
<xsl:value-of select="."/>
<xsl:if test="following-sibling::text()"> ...</xsl:if>
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:temlate>
>
> Q2) How do I write grep2.xsl to meet the following commands
> H:> saxon example.xml grep2.xsl target=Tom
> <doc>hello <name class="matched">Tom</name>. i am <name>Sue</name>.
> cheers</doc>
>
> H:> saxon example.xml grep2.xsl target=am
> <doc class="matched">hello <name>Tom</name>. i am <name>Sue</name>.
> cheers</doc>
>
> H:> saxon example.xml grep2.xsl target=he
> <doc class="matched">hello <name>Tom</name>. i am <name>Sue</name>.
> cheers</doc>
>
>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:for-each select="text()">
<xsl:if test="contains(., $param)">
<xsl:attribute name="class">matched</xsl:attribute>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
Michael Kay
Software AG
home: Michael.H.Kay@ntlworld.com
work: Michael.Kay@softwareag.com
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list