Search code examples
xmlxslt

Conditionally concatinate XML node values via XSLT


I'm very much new to StackOverflow and I've been reading a lot of the XSLT questions but I'm struggling to find an answer to a problem that I'm facing.

I'm currently working with two different XML based OCR output formats (ABBYY and HOCR) and using an XSLT to transform them enough into the the HTML format that the Standard eBook tools can use to output working EPUB. Both OCR formats define pages, paragraphs and lines within the paragraph, so its not difficult assemble lines back and output html paragraph blocks.

Where I'm now running into a problem is trying to re-join paragraphs that get broken across pages back together.

An extremely simplified example of the source document looks like this:

<document xmlns="http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml">
    <page>
        <block>
            <par>This is a line.</par>
            <par>This is a line</par>
            <par>that is split.</par>
        </block>
    </page>
    <page>
        <block>
            <par>line split</par>
        </block>
    </page>
    <page>
        <block>
            <par>across pages.</par>
        </block>
    </page>
</document>

and the ideal output is

<p>This is a line.</p>
<p>This is a line that is split.</p>
<p>line split across pages</p>

I started with this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml" exclude-result-prefixes="x">
    <xsl:output omit-xml-declaration="yes"/>
    <xsl:template match="/">

        <xsl:apply-templates select="x:document/x:page/x:block/x:par"/>

    </xsl:template>
    
    <xsl:template match="x:par">
        <p><xsl:value-of select="."/></p><xsl:text>&#xa;</xsl:text>
    </xsl:template> 

</xsl:stylesheet>

which does the basic transform. The par sub-template, as I understand it, and from trying, doesn't have knowledge of previous or subsequent par nodes.

I've tried moving back a few levels and using a for-each loop, and detecting the end of paragraph from the punctuation (also, simplified for this example), and using following-sibling, but this still has issues, as you don't seem to be able to skip over a node, and, more significantly, the par on the next page doesn't appear to be a sibling to the par on the previous page, which makes sense - its a different branch.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml" exclude-result-prefixes="x">
    <xsl:output omit-xml-declaration="yes"/>
    <xsl:template match="/">

        <xsl:apply-templates select="x:document"/>

    </xsl:template>
    
    <xsl:template match="x:document">
    
        <xsl:for-each select="x:page/x:block/x:par">
            <xsl:choose>
                <xsl:when test="substring(./text(), string-length(./text()))!='.'"> 
                    <p><xsl:apply-templates select="."/><xsl:text> </xsl:text>
                    <xsl:apply-templates select="following-sibling::*[1]"/></p><xsl:text>&#xa;</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <p><xsl:apply-templates select="."/></p><xsl:text>&#xa;</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>

    </xsl:template>     
    
    <xsl:template match="x:par">
        <xsl:value-of select="."/>
    </xsl:template> 

</xsl:stylesheet>

I've also tried using an xsl:if condition to select when to print the <p> tags, but I get errors from xsltproc about mismatched tags.

Is what I'm trying to do actually possible with XSLT? I'm not necessarily wedded to XLST 1.0, either - xsltproc is just an easily available processor. Obviously, order is important as well.


Solution

  • This is how you could do it in XSLT 1.0:

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml" 
    exclude-result-prefixes="x">
    <xsl:output omit-xml-declaration="yes"/>
        
    <xsl:template match="/x:document">
        <xsl:for-each select="x:page/x:block/x:par[substring(., string-length(.)) = '.']">
            <p>
                <xsl:apply-templates select="."/>
            </p>
        </xsl:for-each>
    </xsl:template>
        
    <xsl:template match="x:par">
        <xsl:apply-templates select="preceding::x:par[1][not(substring(., string-length(.)) = '.')]"/>
        <xsl:value-of select="."/>
    </xsl:template> 
    
    </xsl:stylesheet>
    

    Note that the result is an XML fragment, not a well-formed XML document (no single root element).


    For a different (and possibly more efficient) approach, see: https://stackoverflow.com/a/67035437/3016153


    Added:

    To insert a space after each segment except the last, you could do:

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:x="http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml" 
    exclude-result-prefixes="x">
    <xsl:output omit-xml-declaration="yes"/>
        
    <xsl:template match="/x:document">
        <xsl:for-each select="x:page/x:block/x:par[substring(., string-length(.)) = '.']">
            <p>
                <xsl:apply-templates select=".">
                    <xsl:with-param name="last" select="true()"/>
                </xsl:apply-templates>
            </p>
        </xsl:for-each>
    </xsl:template>
        
    <xsl:template match="x:par">
        <xsl:param name="last"/>
        <xsl:apply-templates select="preceding::x:par[1][not(substring(., string-length(.)) = '.')]"/>
        <xsl:value-of select="."/>
        <xsl:if test="not($last)">
            <xsl:text> </xsl:text>
        </xsl:if>
    </xsl:template> 
    
    </xsl:stylesheet>