Search code examples
xmlxslttei

Change style of one single element using XSLT


I am completely new to using XSLT, but in my job they're asking me to do a little XSL Transformation. Please forgive me if my question is too basic.

I work with handwritten poetry and I'd like to see every verse (l element, inside lg) as a line. Verses could include several elements, such as abbreviations (abbr), deletions (del), additions (add) and other elements. I need these elements to be shown in different colors inside each verse.

This is part of the code I've been able to write so far.

<xsl:template match = "TEI/text/body/lg">
  <p><xsl:apply-templates select = "l" /></p>
  </xsl:template>

  <xsl:template match="l" priority="0">
    <span style="color:black;">
      <xsl:value-of select="." />
    </span><br/>
  </xsl:template>

Hope you can help.


Solution

  • Please try the following solution:

    Given this input, which appears to be encoded in the TEI (Text Encoding Initiative) format:

    <TEI xmlns="http://www.tei-c.org/ns/1.0">
      <teiHeader>
          <fileDesc>
             <titleStmt>
                <title>Title</title>
             </titleStmt>
             <publicationStmt>
                <p>Information about publication or distribution</p>
             </publicationStmt>
             <sourceDesc>
                <p>Information about the source</p>
             </sourceDesc>
          </fileDesc>
      </teiHeader>
      <text>
          <body>
             <lg>
                <l>Roses are <abbr>red</abbr></l>
                <l>Violets are <del>blue</del></l>
                <l>Line <add>three</add></l>
             </lg>
             <lg>
                <l>Roses are red</l>
                <l>Line two</l>
                <l>Line three</l>
             </lg>
          </body>
      </text>
    </TEI>
    

    the following stylesheet:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema" 
        xmlns:tei="http://www.tei-c.org/ns/1.0"
        exclude-result-prefixes="xs tei"
        version="1.0">
        <xsl:output method="html" encoding="UTF-8" indent="yes"/>
        
        <xsl:template match="/">
            <div class="wrapper">
                <xsl:apply-templates select="tei:TEI/tei:text/tei:body"/>
            </div>
        </xsl:template>
        
        <!--Block elements-->
        
        <xsl:template match="tei:lg">
            <div class="verse" style="padding: 2rem;">
                <xsl:apply-templates/>
            </div>
        </xsl:template>
        <xsl:template match="tei:l">
            <div class="verse-line">
                <xsl:apply-templates/>
            </div>
        </xsl:template>
        
        <!--Inline elements-->
        
        <xsl:template match="tei:abbr">
            <span class="abbr" style="color: red;">
                <xsl:apply-templates/>
            </span>
        </xsl:template>
        
        <xsl:template match="tei:del">
            <span class="del" style="color: blue;">
                <xsl:apply-templates/>
            </span>
        </xsl:template>
        
        <xsl:template match="tei:add">
            <span class="add" style="color: cyan;">
                <xsl:apply-templates/>
            </span>
        </xsl:template>
        
    </xsl:stylesheet>
    

    will output this HTML:

    <div class="wrapper">
       <div class="verse" style="padding: 2rem;">
          <div class="verse-line">Roses are <span class="abbr" style="color: red;">red</span></div>
          <div class="verse-line">Violets are <span class="del" style="color: blue;">blue</span></div>
          <div class="verse-line">Line <span class="add" style="color: cyan;">three</span></div>
          </div>   
       <div class="verse" style="padding: 2rem;">      
          <div class="verse-line">Roses are red</div>
          <div class="verse-line">Line two</div>
          <div class="verse-line">Line three</div>
       </div>
    </div>
    

    It will work with any of the follwing XSLT processors:

    • Saxon6.5.5
    • Saxon-HE 9.9.1.7
    • Saxon-PE 9.9.1.7
    • Saxon-EE 9.9.1.7