Re: [xsl] Breaking paragraphs one linebreaks

Subject: Re: [xsl] Breaking paragraphs one linebreaks
From: "Terry Badger terry_badger@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 11 May 2019 15:11:42 -0000
Try this. It is easier for me to understand.
<?xml version="1.0"?>
<!-- terry badger 2019-05-11 use regex to separate types of text then
repackage in new collection order -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0">
B B B B <xsl:output encoding="utf-8" indent="yes"/>
B B B B <xsl:strip-space elements="*"/>
B B B B <!--
==========================================================================-->
B B B B <!--variable with content regrouped into multiple parts for each seg
-->
B B B B <xsl:variable name="packaged">
B B B B B B B B <xsl:element name="wrapper">
B B B B B B B B B B B B <xsl:for-each select="//seg">
B B B B B B B B B B B B B B B B <xsl:copy>
B B B B B B B B B B B B B B B B B B B B <xsl:attribute name="xml:lang"
select="parent::*/@xml:lang"/>
B B B B B B B B B B B B B B B B B B B B <xsl:analyze-string select="."
regex="&lt;br&gt;">
B B B B B B B B B B B B B B B B B B B B B B B B <xsl:non-matching-substring>
B B B B B B B B B B B B B B B B B B B B B B B B B B B B <xsl:element
name="part">
B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B <xsl:copy-of
select="."/>
B B B B B B B B B B B B B B B B B B B B B B B B B B B B </xsl:element>
B B B B B B B B B B B B B B B B B B B B B B B B </xsl:non-matching-substring>
B B B B B B B B B B B B B B B B B B B B </xsl:analyze-string>
B B B B B B B B B B B B B B B B </xsl:copy>
B B B B B B B B B B B B </xsl:for-each>
B B B B B B B B </xsl:element>
B B B B </xsl:variable>
B B B B <!--
==========================================================================-->
B B B B <!-- start at root and output a result document to make it easier to
see -->
B B B B <xsl:template match="/">
B B B B B B B B <xsl:result-document href="output.xml">
B B B B B B B B B B B B <xsl:apply-templates/>
B B B B B B B B </xsl:result-document>
B B B B </xsl:template>
B B B B <!--
==========================================================================-->
B B B B <xsl:template match="tmx | body | header">
B B B B B B B B <xsl:copy>
B B B B B B B B B B B B <xsl:copy-of select="@*"/>
B B B B B B B B B B B B <xsl:apply-templates/>
B B B B B B B B </xsl:copy>
B B B B </xsl:template>
B B B B <!--
==========================================================================-->
B B B B <xsl:template match="tu">
B B B B B B B B <xsl:for-each select="$packaged/wrapper/seg[1]/part">
B B B B B B B B B B B B <xsl:variable name="part-order" select="position()"/>
B B B B B B B B B B B B <xsl:element name="tu">
B B B B B B B B B B B B B B B B <xsl:attribute name="tuid"
select="position()"/>
B B B B B B B B B B B B B B B B <xsl:for-each select="$packaged/wrapper/seg">
B B B B B B B B B B B B B B B B B B B B <xsl:element name="tuv">
B B B B B B B B B B B B B B B B B B B B B B B B <xsl:attribute name="xml:lang"
select="@xml:lang"/>
B B B B B B B B B B B B B B B B B B B B B B B B <xsl:element name="seg">
B B B B B B B B B B B B B B B B B B B B B B B B B B B B <xsl:value-of
select="normalize-space(part[position() = $part-order])"/>
B B B B B B B B B B B B B B B B B B B B B B B B </xsl:element>
B B B B B B B B B B B B B B B B B B B B </xsl:element>
B B B B B B B B B B B B B B B B </xsl:for-each>
B B B B B B B B B B B B </xsl:element>
B B B B B B B B </xsl:for-each>
B B B B </xsl:template>
</xsl:stylesheet>

Terry






On bThursdayb, bMayb b9b, b2019b b04b:b16b:b36b
bPMb bEDT, Martin Honnen martin.honnen@xxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:





Am 09.05.2019 um 21:55 schrieb Martin Honnen martin.honnen@xxxxxx:
> Am 09.05.2019 um 21:42 schrieb Manuel Souto Pico terminolator@xxxxxxxxx:
>>
>>
>> @Martin, your example works really well. I had to edit the expression,
>> as in my real files sometimes they have used lists instead of
>> linebreaks:
>>
>> <xsl:param name="lb"
>> as="xs:string">&lt;/?(li|ul|br)\s*/?&gt;</xsl:param>
>>
>> However, I can see what I would also need to split at the end of
>> sentences when there's no escaped tag but just final punctuation. To
>> avoid the transformation eating the punctuation, I have tried with a
>> lookbehind assertion but it seems it's not supported:
>>
>> <xsl:param name="lb"
>> as="xs:string">(?<=[.!?])\s|&lt;/?(li|ul|br)\s*/?&gt;</xsl:param>
>>
>> Any ideas?
>>
>
> In general, if there is markup, it might be better to try to parse it,
> in your initial sample you seemed to have simple HTML empty element
> syntax with <br> elements, now with the adapted regular expression it
> seems you expect opening and closing tags.
>
> If you know the escaped markup is an XML fragment then I would try to
> parse it with the "parse-xml-fragment" function, if it is HTML, then I
> would look into using David Carlisle's HTML parser implementation done
> in pure XSLT 2 or use an extension function like the commercial editions
> of Saxon offer.
>
> After parsing, you can then apply normal templates or grouping
> constructs.
>
An adaption of the previous suggestion, but now with escaped XML syntax
in the sample input, to then use parse-xml-fragment, is at

https://xsltfiddle.liberty-development.net/ej9EGcD/5

and does

B  <xsl:template match="tu">
B B B B B  <xsl:variable name="split">
B B B B B B B B B  <xsl:apply-templates mode="split"/>
B B B B B  </xsl:variable>
B B B B B  <xsl:for-each-group select="$split/tuv/seg" group-by="position()
mod count($split/tuv[1]/seg)">
B B B B B B B B B  <tu tuid="{position()}">
B B B B B B B B B B B B B  <xsl:apply-templates
select="current-group()/snapshot()/.."/>
B B B B B B B B B  </tu>
B B B B B  </xsl:for-each-group>
B  </xsl:template>

B  <xsl:mode name="split" on-no-match="shallow-copy"/>

B  <xsl:template match="seg" expand-text="yes" mode="split">
B B B B B  <xsl:for-each-group select="parse-xml-fragment(.)/node()"
group-ending-with="br">
B B B B B B B B B  <seg>{.}</seg>
B B B B B  </xsl:for-each-group>
B  </xsl:template>

For HTML parsing you would need to use an extension or David Carlisle's
HTML parser available on Github, but the approach then is the same. Of
course handling different elements like various list constructs needs
more code but once you have a tree you can process the "normal" XSLT way
you can write more templates and/or more modes for various processing
steps to address more complex input structures.

Current Thread