How to use two different lines of analysis for one node

In my XSLT transformation, I have two lines of analysis that I need to use to process a single node. They work great one by one, but I don’t know how to assemble them.

The XML document is as follows:

<article>
    <title>Article 1</title>
    <text><![CDATA[Lorem ipsum dolor sit amet, s consectetur adipiscing elit. Donec lorem diam, eleifend sed mollis id, condimentum in velit.

Sed sit amet erat ac mauris adipiscing elementum. Pellentesque eget quam augue, id faucibus magna.

Ut malesuada arcu eu elit sodales sodales. Morbi tristique porttitor tristique. Praesent eget vulputate dui. Cras ut tortor massa, at faucibus ligula.]]></text>
</article>

Here is my XSLT:

<xsl:template match="/">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <title>Page title</title>
        </head>
        <body>
            <xsl:for-each select="article">
                <h1><xsl:value-of select="./title"/></h1>

                <!-- This adds paragraphs tags instead of empty lines in the text -->
                <xsl:analyze-string select="./text" regex="&#xa;">
                    <xsl:non-matching-substring>
                        <p>
                            <xsl:value-of select="." disable-output-escaping="yes"/>
                        </p>
                    </xsl:non-matching-substring>
                </xsl:analyze-string> 

                <!-- This is Czech language specific. It looks for ' s ' (or other letter) and changes second space for &nbsp;. So after that it is ' s&nbsp;'. -->
                <xsl:analyze-string select="./text" regex="(\s[k/K/s/S/v/V/z/Z]\s)">
                    <xsl:matching-substring>
                        <xsl:text> </xsl:text>
                        <xsl:value-of select="replace(., ' ','')" disable-output-escaping="yes"/>
                        <xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text>
                    </xsl:matching-substring>
                    <xsl:non-matching-substring>
                        <xsl:value-of select="." disable-output-escaping="yes"/>
                    </xsl:non-matching-substring>
                </xsl:analyze-string>
            </xsl:for-each>
        </body>
    </html>
</xsl:template>

I need to apply both analytic lines in the generated text, as well as tags <p>for paragraphs, and also add them &nbsp;to the right places.

My desired result would look like this:

<h1>Article 1</h1>    
<p>Lorem ipsum dolor sit amet, s&nbsp;consectetur adipiscing elit. Donec lorem diam, eleifend sed mollis id, condimentum in velit.</p>
<p>Sed sit amet erat ac mauris adipiscing elementum. Pellentesque eget quam augue, id faucibus magna.</p>
<p>Ut malesuada arcu eu elit sodales sodales. Morbi tristique porttitor tristique. Praesent eget vulputate dui. Cras ut tortor massa, at faucibus ligula.</p>

Any ideas how to do this? Thank you for taking the time and trying to help me.

+3
source share
3 answers

Here is my setup for Dimitre solution:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" indent="yes" encoding="UTF-8"/>

 <xsl:template match="/*/text">
   <xsl:for-each select="tokenize( replace(., '\s([kKsSvVzZ])\s', ' $1&#xA0;'), '\n')">
     <p><xsl:value-of select="."/></p>
  </xsl:for-each>
 </xsl:template>

 <xsl:template match="title">
  <h1><xsl:value-of select="."/></h1>
 </xsl:template>
</xsl:stylesheet>

Notes

  • , " s/S/v/V/k/K/z/Z". . . , [sSvVkKzZ]
  • , , UTF-8 , ASCII.
  • , , html.
  • html , . html.
  • fn: tokenise() xsl: analysis-string/xsl: non-matching-substring, .
  • Saxon.
  • . , replace() xsl: , .
  • , -- = "". , , , , , . HTML- HTML-, CDATA. - HTML . , . , ?
+3

:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes" encoding="ascii"/>

 <xsl:template match="/*/text">
  <xsl:analyze-string select=
   "replace(., '\ss\s', ' s&#xA0;')"
   regex="&#xA;">
    <xsl:non-matching-substring>
     <p><xsl:sequence select="."/></p>
    </xsl:non-matching-substring>
  </xsl:analyze-string>
 </xsl:template>

 <xsl:template match="title">
  <h1><xsl:value-of select="."/></h1>
 </xsl:template>
</xsl:stylesheet>

XML-:

<article>
  <title>Article 1</title>
<text><![CDATA[Lorem ipsum dolor sit amet, s consectetur adipiscing elit. Donec lorem diam, eleifend sed mollis id, condimentum in velit.
Sed sit amet erat ac mauris adipiscing elementum. Pellentesque eget quam augue, id faucibus magna.
Ut malesuada arcu eu elit sodales sodales. Morbi tristique porttitor tristique. Praesent eget vulputate dui. Cras ut tortor massa, at faucibus ligula.]]></text>
</article>

, :

  <h1>Article 1</h1>
<p>Lorem ipsum dolor sit amet, s&#160;consectetur adipiscing elit. Donec lorem diam, eleifend sed mollis id, condimentum in velit.</p>
<p>Sed sit amet erat ac mauris adipiscing elementum. Pellentesque eget quam augue, id faucibus magna.</p>
<p>Ut malesuada arcu eu elit sodales sodales. Morbi tristique porttitor tristique. Praesent eget vulputate dui. Cras ut tortor massa, at faucibus ligula.</p>

. DOE, XSLT 2.0, , XSLT 2.0 DOE. .

:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"
  encoding="ascii" use-character-maps="nbsp"/>

 <xsl:character-map name="nbsp">
  <xsl:output-character
  character="&#xA0;" string="&amp;nbsp;"/>
 </xsl:character-map>

 <xsl:template match="/*/text">
  <xsl:analyze-string select=
   "replace(., '\ss\s', ' s&#xA0;')"
   regex="&#xA;">
    <xsl:non-matching-substring>
     <p><xsl:sequence select="."/></p>
    </xsl:non-matching-substring>
  </xsl:analyze-string>
 </xsl:template>

 <xsl:template match="title">
  <h1><xsl:value-of select="."/></h1>
 </xsl:template>
</xsl:stylesheet>

XML (. ), , :

  <h1>Article 1</h1>
<p>Lorem ipsum dolor sit amet, s&nbsp;consectetur adipiscing elit. Donec lorem diam, eleifend sed mollis id, condimentum in velit.</p>
<p>Sed sit amet erat ac mauris adipiscing elementum. Pellentesque eget quam augue, id faucibus magna.</p>
<p>Ut malesuada arcu eu elit sodales sodales. Morbi tristique porttitor tristique. Praesent eget vulputate dui. Cras ut tortor massa, at faucibus ligula.</p>
+3

, , xsl: analy-string . , , xsl: analy-string .

<xsl:function name="f:one" as="xs:string">
  <xsl:param name="in" as="xs:string">
  <xsl:analyze-string select="in".../>
</xsl:function>

<xsl:function name="f:two" as="xs:string">
  <xsl:param name="in" as="xs:string">
  <xsl:analyze-string select="in".../>
</xsl:function>

... select="f:two(f:one(.))"...

However, in your case this is simpler because the first line of xsl: analysis can be done with a simple call to replace ().

+2
source

All Articles