dxh405 / hughes_project

Repo for Langston Hughes final project in DIGIT 110
https://dxh405.github.io/hughes_project/
1 stars 0 forks source link

Highlighting phrases with xsl:analyze-string #3

Open ebeshero opened 3 years ago

ebeshero commented 3 years ago

I wanted to share the XSLT code I was working on in class yesterday, with some comments about how the <xsl:analyze-string> works. Also, I made a correction from the code I wrote in class: In order to make sure all your templates for indents are working, I needed to write an xsl:template to match on text() nodes instead of the whole <line> elements. Here's my template rule that matches on just the phrase "dream deferred" wherever it shows up in a text() node of an element in your XML:

    <xsl:template match="text()">
        <xsl:analyze-string select="." regex="dream deferred">
            <xsl:matching-substring> 
                <span class="motif"><xsl:value-of select="."/></span>
            </xsl:matching-substring>
            <xsl:non-matching-substring>
                <xsl:value-of select="."/>
            </xsl:non-matching-substring>
        </xsl:analyze-string>
    </xsl:template>

This template matches on text() nodes anywhere in your source XML, and analyzes them looking for the regex pattern "dream deferred", as a literal phrase. Anytime it finds those characters all together, it outputs an HTML <span class="motif">...</span> around the matching part, and it also outputs the non-matching part.

But then, I wanted to see if we could capture more phrases. . . @dxh405 @wdjacca @AtomicOlsen @amayadwillis @BarbieCessar

ebeshero commented 3 years ago

To see if we can capture another phrase or pattern with xsl:analyze-string, I experimented to see if we could find the word "freedom" either capitalized or lower-cased. What we have to do is set up a series of analyze-string actions in a nested way.

  1. So you first look for one phrase ("dream deferred"), and output the matching substring with HTML tags as you wish, and you also output a non-matching substring so you don't lose the rest of the text() node.

  2. We turn to the non-matching substring to analyze it further! We set up a new <xsl:analyze-string> to nest inside the <xsl:non-matching-substring> of the previous <xsl:analyze-string>.

You could totally keep on going with this. The gist of it all is that you need to do all the XSLT string analysis in one nested series of actions: Look for something, find a match, output the non-match too, and then keep analyzing the non-matches.

So here is how I revised the <xsl:analyze-string> I first wrote, to keep on matching either "freedom" or "Freedom". I really used a regex pattern this time to match either "F" or "f" with [Ff]reedom:

   <xsl:template match="text()">
        <xsl:analyze-string select="." regex="dream deferred">
            <xsl:matching-substring> 
                <span class="motif"><xsl:value-of select="."/></span>
            </xsl:matching-substring>

            <xsl:non-matching-substring><!--ebb: This is the first non-matching substring -->

                <xsl:analyze-string select="." regex="[Ff]reedom"><!--ebb: I kept on going here to see if I could keep adding highlights to other phrases. This is how you do it.
                Set a new xsl: analyze string inside the non-matching substring, and keep on going, so each new one nests inside the non-matching substring of the previous analyze-string.-->

                    <xsl:matching-substring>
                        <span class="motif"><xsl:value-of select="."/></span>
                    </xsl:matching-substring>

                    <xsl:non-matching-substring><!--ebb: This is the second non-matching substring. If I wanted to keep  looking for more phrases, I would set yet another xsl:analyze-string inside here.-->
                        <xsl:value-of select="."/>
                    </xsl:non-matching-substring>
                </xsl:analyze-string>

            </xsl:non-matching-substring>
        </xsl:analyze-string>    
    </xsl:template>

@dxh405 @wdjacca @AtomicOlsen @amayadwillis @BarbieCessar