Re: [xsl] problem with transforming mixed content

Subject: Re: [xsl] problem with transforming mixed content
From: "Wolfhart Totschnig wolfhart.totschnig@xxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 15 Aug 2020 23:20:13 -0000
Thank you very much to everybody who replied to my post! I learned a lot from working through the solutions you proposed. In the end, I decided to go with Graydon's "type (a)" solution (i.e., the one below) since the stylesheet is indeed supposed to "deal with the world indefinitely". (Specifically, it is supposed to deal with the DOI metadata supplied by www.crossref.org, which, from what I have observed so far, is rather messy and might turn out to be even messier.) I understand that Gerrit's "upward projection" solution would be even better, but that one seems too daunting for me at the moment.

Thanks again,
Wolfhart


On 15.08.20 12:43, Graydon graydon@xxxxxxxxx wrote:
On Sat, Aug 15, 2020 at 04:03:26PM -0000, Wolfhart Totschnig wolfhart.totschnig@xxxxxxxxxxx scripsit:
And thank you, Michael, for the detailed explanation of possible approaches
to the problem. Graydon's solution will work very well in my case, I think,
since I can test for most error-producing conditions before applying the
code and the probability of further errors seems sufficiently low in my
context and for my purposes. But I am still curious: What would an approach
of type (a) look like in my case? It seems to me that implementing this
approach would again face the original problem: "turning the punctuation
into markup" sounds like a description of the original problem.
I tend to make a distinction between conversion code -- I'm going to do
this once, for this exact data set -- and production code -- the code
has to go deal with the world indefinitely.  Approach a) is definitely
more like production code than approach b).

Dr. Kay is completely correct that the approach b) solution I provided
has a lot of ways to fail.  In a one-time data conversion context, when
I've already used XQuery to find out exactly what's in there, I wouldn't
worry about that.  I'm trying to minimize the effort required to write
the one-use conversion code.

In a production context -- the code must go fend for itself -- approach
a) wins.

For your example, approach a) would look like:

<xsl:stylesheet exclude-result-prefixes="xs math xd" version="3.0"
   xmlns:math="http://www.w3.org/2005/xpath-functions/math"; xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl";
   xmlns:xs="http://www.w3.org/2001/XMLSchema"; xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
   <xd:doc scope="stylesheet">
     <xd:desc>
       <xd:p><xd:b>Created on:</xd:b> Aug 15, 2020</xd:p>
       <xd:p><xd:b>Author:</xd:b> graydon</xd:p>
       <xd:p />
     </xd:desc>
   </xd:doc>
   <xsl:variable as="element(title)+" name="test">
     <title>THE TITLE OF THE BOOK WITH SOME <i>ITALICS</i> AND SOME MORE WORDS: THE SUBTITLE OF THE BOOK WITH SOME
         <i>ITALICS</i></title>
   </xsl:variable>
   <xd:doc>
     <xd:desc>test</xd:desc>
   </xd:doc>
   <xsl:template name="xsl:initial-template">
     <bucket>
       <xsl:for-each select="$test">
         <xsl:variable as="element()" name="temp1">
           <xsl:apply-templates mode="marker" select="." />
         </xsl:variable>
         <xsl:variable as="element()+" name="temp2">
           <xsl:apply-templates mode="split" select="$temp1" />
         </xsl:variable>
         <xsl:sequence select="$temp2" />
       </xsl:for-each>
     </bucket>
   </xsl:template>
   <xsl:mode name="marker" on-no-match="shallow-copy" />
   <xsl:mode name="split" on-no-match="shallow-copy" />
   <xsl:mode name="recase" on-no-match="shallow-copy" />
   <xd:doc>
     <xd:desc>place the separator marker element; this can get much more involved if you aren't sure you certainly have a single colon in a text node</xd:desc>
   </xd:doc>
   <xsl:template match="text()[contains(., ':')]" mode="marker">
     <xsl:value-of select="substring-before(., ':')" />
     <title-separator />
     <xsl:value-of select="substring-after(., ':') => replace('^\p{Zs}+', '')" />
   </xsl:template>
   <xd:doc>
     <xd:desc>divide title into title and subtitle</xd:desc>
   </xd:doc>
   <xsl:template match="title" mode="split">
     <title>
       <xsl:apply-templates mode="recase" select="descendant::node()[following::title-separator]" />
     </title>
     <subtitle>
       <xsl:apply-templates mode="recase" select="descendant::node()[preceding::title-separator]" />
     </subtitle>
   </xsl:template>
   <xd:doc>
     <xd:desc>lower case all the text nodes in title</xd:desc>
   </xd:doc>
   <xsl:template match="text()" mode="recase">
     <xsl:sequence select="lower-case(.)" />
   </xsl:template>
</xsl:stylesheet>

You could add more <title/> elements to $test and test all of them as
you find problematic title elements in the content set.

Current Thread