Cut XML text string(s) for usage in XHTML paragraph sentences

I am looking to extract sentence(s) from same XML node. Each sentence(s) will end up in XHTML result file paragraph tags. The amount of sentence(s) that goes into each XHTML paragraph, might vary. It is always each dot that controls the end of a sentence. The XML data can be spread on different lines. The amount of sentence in the XML node can be larger then explained in below code so I cannot hardcode the positions of the different sentences.

I managed to locate the first sentence by "substring-before" the first dot.

You find below code here: https://xsltfiddle.liberty-development.net/asoTJE/1

XML

<?xml version="1.0" encoding="utf-8" ?>

<library>
    <books>
        <chapters>
            <chapterOne>
                A piece of text. Another string. 
                Something, else here.
                </chapterOne>
        </chapters>
    </books>
</library>

XSL

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
    xmlns="http://www.example.org/1"
    >

  <xsl:output method="xhtml" indent="yes" html-version="5"/>

  <xsl:template match="/library/books/chapters/chapterOne">
    
    <html>
        <head><title>MyTitle</title></head>
        <body>
            
            <!-- Works -->
            <p>
                Sentence-1. First sentence (in same chapter): 
                Cut after found first dot.
                <xsl:value-of select="substring-before(., '.')" />.
            </p>
    
            <!-- Not yet found a way to solve -->
    
            <p>
                
                Sentence-3 (in same chapter): 
                <xsl:value-of select="." />
            </p>
            
            <!-- Not yet found a way to solve -->
            
            <p>
                Extracted sentences, e.g. sentence-1 and sentence-2 (in same chapter): 
                Cut after second dot.
                <xsl:value-of select="." />
            </p>
            
            <!-- Not yet found a way to solve -->
            
            <p>
                Extracted sentences, e.g. sentence-1 and sentence-3 (in same chapter): 
                <xsl:value-of select="." />
            </p>
            
        </body>
    </html>
    
    
  </xsl:template>
  
</xsl:stylesheet>

XHTML result

<?xml version="1.0" encoding="UTF-8"?><html xmlns="http://www.example.org/1">
   <head>
      <title>MyTitle</title>
   </head>
   <body>
      <p>
         Sentence-1. First sentence (in same chapter): 
         Cut after found first dot.
         
         A piece of text.
         
      </p>
      <p>
         
         Sentence-3 (in same chapter): 
         
         A piece of text. Another string. 
         Something, else here.
         
      </p>
      <p>
         Extracted sentences, e.g. sentence-1 and sentence-2 (in same chapter): 
         Cut after second dot.
         
         A piece of text. Another string. 
         Something, else here.
         
      </p>
      <p>
         Extracted sentences, e.g. sentence-1 and sentence-3 (in same chapter): 
         
         A piece of text. Another string. 
         Something, else here.
         
      </p>
   </body>
</html>


Read more here: https://stackoverflow.com/questions/67398483/cut-xml-text-strings-for-usage-in-xhtml-paragraph-sentences

Content Attribution

This content was originally published by Toolbox at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: