Conditionally merge XML node values via XSLT

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><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>
</code>
<code><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> </code>
<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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><p>This is a line.</p>
<p>This is a line that is split.</p>
<p>line split across pages</p>
</code>
<code><p>This is a line.</p> <p>This is a line that is split.</p> <p>line split across pages</p> </code>
<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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><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>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
</code>
<code><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> </xsl:text> </xsl:template> </xsl:stylesheet> </code>
<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>
</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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><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="."/></p><xsl:text>
</xsl:text>
<p><xsl:apply-templates select="following-sibling::*[1]"/></p><xsl:text>
</xsl:text>
</xsl:when>
<xsl:otherwise>
<p><xsl:apply-templates select="."/></p><xsl:text>
</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template match="x:par">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
</code>
<code><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="."/></p><xsl:text> </xsl:text> <p><xsl:apply-templates select="following-sibling::*[1]"/></p><xsl:text> </xsl:text> </xsl:when> <xsl:otherwise> <p><xsl:apply-templates select="."/></p><xsl:text> </xsl:text> </xsl:otherwise> </xsl:choose> </xsl:for-each> </xsl:template> <xsl:template match="x:par"> <xsl:value-of select="."/> </xsl:template> </xsl:stylesheet> </code>
<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="."/></p><xsl:text>
</xsl:text>
                    <p><xsl:apply-templates select="following-sibling::*[1]"/></p><xsl:text>
</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <p><xsl:apply-templates select="."/></p><xsl:text>
</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.

New contributor

Simon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật