I have made some progress on this, not to a working point yet but I'm more
confident than I was, so thanks to all for the suggestions which have been
helpful. I also found some hints in a stackoverflow answer of Martin Honnen's
which reinforced the advice to work on this by adding a line marker element and
using grouping.
The original statement of the requirement was a bit vague, and the content
model currently in use is a bit too flexible. So I think I can stipulate that
inline elements will not run across line breaks (and if they do I should be
able to run a pre-fix which splits them), nor will the content include any
nested inline elements.
At the moment I'm assuming that in the step where I insert line marker
elements, I also have to use modal templates to insert inline element markers,
then run another pass to restore the inline elements. Something like this,
correct?
<xsl:variable name="brokenlines">
<xsl:element name="textlines">
<xsl:element name="linemarker"/>
<xsl:analyze-string select="." regex="(\r\n?|\n\r?)">
<xsl:matching-substring>
<xsl:element name="linemarker"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:apply-templates mode="break"/>
</xsl:non-matching-substring>
</xsl:analyze-string>
<xsl:element>
</xsl:variable>
<xsl:variable name="textlines">
<xsl:call-template name="rebuild">
<xsl:with-param name="lines" select="$brokenlines"/>
</xsl:call-template>
<xsl:variable>
<-- $textlines/textlines is now the original textlines with line children -->
...
<xsl:template match="textlines/*/text()" mode="break">
<xsl:value-of select="concat('[[{', name(..), '}', ., ']]')" />
</xsl:template>
<xsl:template name="rebuild">
<xsl:param name="lines" as="document-node()" />
<xsl:element name="textlines">
<xsl:for-each select="$lines/textlines">
<xsl:for-each-group select="node()" group-starting-with="linemarker">
<xsl:element name="line">
<xsl:apply-templates
select="current-group()[not(self::linemarker)]" mode="rebuild" />
</xsl:element>
</xsl:for-each-group>
</xsl:element>
</xsl:template>
<xsl:template match="text()" mode="rebuild">
<xsl:analyze-string select="." regex="something matching [[{name}content]]">
<xsl:matching-substring>
<xsl:element name="the name in the regex">
the content in the regex
</xsl:element>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl-value-of select="." />
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
Am I going along the right lines? I'd prefer to be set straight sooner rather
than later!
Cheers
T
-----Original Message-----
From: Michael Müller-Hillebrand mmh(_at_)docufy(_dot_)de
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>
Sent: Friday, 7 May 2021 20:26
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] Seeking a smarter tokenize for augmented text
Hi,
From Gerrit I would have expected something like
<xsl:for-each-group select="node()" group-starting-with="text()[matches(.,
'^
')]">
...
</xsl:for-each-group>
but it is possibly too much hassle to handle corner cases.
Because I assume it is very likely that inline elements may run across line
breaks, it is time for Gerrit’s "Upward Projection", see
https://archive.xmlprague.cz/2019/files/xmlprague-2019-proceedings.pdf#page=347
- Michael MH
Am 06.05.2021 um 11:14 schrieb Imsieke, Gerrit, le-tex
gerrit(_dot_)imsieke(_at_)le-tex(_dot_)de
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>:
Hi Trevor,
If the markup in the lines doesn't stretch across line breaks, you can insert
empty elements for each line break using xsl:analyze-string in a first pass.
In the second pass, you then group the nodes below textlines, starting or
ending with such an empty linebreak element that you introduced in the first
pass.
Gerrit
On 06.05.2021 11:09, Trevor Nicholls trevor(_at_)castingthevoid(_dot_)com
wrote:
Hi
Turning xml like this:
<textlines>this is line 1
this is line 2
this is line 3</textlines>
into something like this:
<textlines>
<line>this is line 1</line>
<line>this is line 2</line>
<line>this is line 3</line>
</textlines>
is simple and I have functions that do it. But they rely on <textlines>
being an xs:string, which is processed by a trivial function I wrote:
<xsl:function name="my:intoLines" as="xs:string*">
<xsl:param name="arg" as="xs:string?" />
<xsl:sequence select="tokenize( $arg, '(\r\n?|\n\r?)' )" />
</xsl:function> I would like to extend this so that it can handle an
element consisting of text lines with some embedded markup (there won't be
much, but there will be some). For example, taking:
<textlines>this is line <seq>1</seq>
this is <var>line</var> 2
this <emph>is</emph> line 3</textlines> and producing <textlines>
<line>this is line <seq>1</seq></line> <line>this is <var>line</var>
2</line> <line>this <emph>is</emph> line 3</line> </textlines>
Sequence and tokenize isn't adequate but is there a way to do this that
won't overtax my brain too much?
cheers
T
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--