How to split text and save HTML tags (XSLT 2.0)

I have an xml that has a node description:

<config>
  <desc>A <b>first</b> sentence here. The second sentence with some link <a href="myurl">The link</a>. The <u>third</u> one.</desc>
</config>

I am trying to separate sentences using a period as a separator, but at the same time storing the output HTML tags in HTML. I am still a template that breaks the description, but HTML tags are lost due to the normalize-space and substring-before functions. My current template is listed below:

<xsl:template name="output-tokens">
  <xsl:param name="sourceText" />

  <!-- Force a . at the end -->
  <xsl:variable name="newlist" select="concat(normalize-space($sourceText), ' ')" />
  <!-- Check if we have really a point at the end -->
  <xsl:choose>
    <xsl:when test ="contains($newlist, '.')">
      <!-- Find the first . in the string -->
      <xsl:variable name="first" select="substring-before($newlist, '.')" />

      <!-- Get the remaining text -->
      <xsl:variable name="remaining" select="substring-after($newlist, '.')" />
      <!-- Check if our string is not in fact a . or an empty string -->
      <xsl:if test="normalize-space($first)!='.' and normalize-space($first)!=''">
        <p><xsl:value-of select="normalize-space($first)" />.</p>
      </xsl:if>
      <!-- Recursively apply the template for the remaining text -->
      <xsl:if test="$remaining">
        <xsl:call-template name="output-tokens">
          <xsl:with-param name="sourceText" select="$remaining" />
        </xsl:call-template>
      </xsl:if>
    </xsl:when>
    <!--If no . was found -->
    <xsl:otherwise>
      <p>
        <!-- If the string does not contains a . then display the text but avoid 
           displaying empty strings 
         -->
        <xsl:if test="normalize-space($sourceText)!=''">
          <xsl:value-of select="normalize-space($sourceText)" />.
        </xsl:if>
      </p>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

and I use it as follows:

<xsl:template match="config">
  <xsl:call-template name="output-tokens">
       <xsl:with-param name="sourceText" select="desc" />
  </xsl:call-template>
</xsl:template>

Expected Result:

<p>A <b>first</b> sentence here.</p>
<p>The second sentence with some link <a href="myurl">The link</a>.</p>
<p>The <u>third</u> one.</p>
+3
source share
4 answers

Here is one way to implement the second approach suggested by Michael Kay using XSLT 2.

, <stop/> , , <stop/> .

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

  <xsl:output method="xml" indent="yes"/>

  <!-- two-pass processing -->
  <xsl:template match="/">
    <xsl:variable name="intermediate">
      <xsl:apply-templates mode="phase-1"/>
    </xsl:variable>
    <xsl:apply-templates select="$intermediate" mode="phase-2"/>
  </xsl:template>

  <!-- identity transform -->
  <xsl:template match="@*|node()" mode="#all" priority="-1">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" mode="#current"/>
    </xsl:copy>
  </xsl:template>

  <!-- phase 1 -->

  <!-- insert <stop/> "milestone markup" after each sentence -->
  <xsl:template match="text()" mode="phase-1">
    <xsl:analyze-string select="." regex="\.\s+">
      <xsl:matching-substring>
        <xsl:value-of select="regex-group(0)"/>
        <stop/>
      </xsl:matching-substring>
      <xsl:non-matching-substring>
        <xsl:value-of select="."/>
      </xsl:non-matching-substring>
    </xsl:analyze-string>
  </xsl:template>

  <!-- phase 2 -->

  <!-- turn each <stop/>-terminated group into a paragraph -->
  <xsl:template match="*[stop]" mode="phase-2">
    <xsl:copy>
      <xsl:for-each-group select="node()" group-ending-with="stop">
        <p>
          <xsl:apply-templates select="current-group()" mode="#current"/>
        </p>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>

  <!-- remove the <stop/> markers -->
  <xsl:template match="stop" mode="phase-2"/>

</xsl:stylesheet>
+3

, . , , XSLT 1.0 ( , ).

. .

, (, <b>first</b> [b]first[/b]), (xsl: analysis-string), , .

( ) , ( "." <stop/>), ( < xsl:for-each-group group-ending-with="stop"/> . )

+4

, @ .

@Jukka ( ) xsl:analyse-string, XPath 1.0 contains substring-after . config.

:

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

    <xsl:output method="xml" indent="yes"/>

    <!-- two pass processing -->
    <xsl:template match="config">
        <xsl:variable name="pass1">
            <xsl:apply-templates select="node()"/>
        </xsl:variable>
        <xsl:apply-templates mode="pass2" select="$pass1/*"/>
    </xsl:template>

    <!-- 1. Copy everything as is (identity) -->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <!-- 1. Replace "text. text" with "text<dot/> text" -->
    <xsl:template match="text()[contains(.,'. ')]">
        <xsl:value-of select="substring-before(.,'. ')"/>
        <dot/>
        <xsl:value-of select="substring-after(.,'. ')"/>
    </xsl:template>

    <!-- 2. Group by examining in population order ending with dot -->
    <xsl:template match="desc" mode="pass2">
        <xsl:for-each-group select="node()" 
            group-ending-with="dot">
            <p><xsl:apply-templates select="current-group()" mode="pass2"/></p>
        </xsl:for-each-group>
    </xsl:template>

    <!-- 2. Identity -->
    <xsl:template match="node()|@*" mode="pass2">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" mode="pass2"/>
        </xsl:copy>
    </xsl:template>

    <!-- 2. Replace dot with mark -->
    <xsl:template match="dot" mode="pass2">
        <xsl:text>.</xsl:text>
    </xsl:template>

</xsl:stylesheet>

, , :

<p>A <b>first</b> sentence here.</p>
<p>The second sentence with some link <a href="myurl">The link</a>.</p>
<p>The <u>third</u> one.</p>
+2

All Articles