You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用XSL转换ABBYY FineReader复杂XML至简化XML时输出不符合预期的技术求助

Fixing Your ABBYY FineReader XML to Simplified XML XSL Transformation

Let's break down what's going wrong with your current XSL and fix it step by step:

Issues with Your Current XSL Code

  1. Hardcoded single page/block: You manually created one <page> and <block> element in your root template, instead of iterating over all the <page> and <block> elements in your source XML. That's why all content ends up crammed into a single page/block.
  2. Absolute XPath in loops: When you use paths like /document/page/block/text inside <xsl:for-each>, you're selecting every matching element across the entire document every time, not just the child elements of the current context you're working with.
  3. Incorrect attribute selection: Your blockType variable grabs all blockType attributes from every block in the document at once, which is why you get the weird blockType="Text Text" value.
  4. Missing text source: The actual readable text lives inside the <formatting> element under <line>, not directly in the <line> tag itself.

Corrected XSL Code

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xpath-default-namespace="http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml">
    <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
    
    <!-- Match the root document and create our simplified root element -->
    <xsl:template match="/document">
        <document>
            <!-- Process every page in the source XML -->
            <xsl:apply-templates select="page"/>
        </document>
    </xsl:template>
    
    <!-- Match each source page and create a simplified page element -->
    <xsl:template match="page">
        <page>
            <!-- Process every block inside the current page -->
            <xsl:apply-templates select="block"/>
        </page>
    </xsl:template>
    
    <!-- Match each source block, keep the blockType attribute, process child text elements -->
    <xsl:template match="block">
        <block blockType="{@blockType}">
            <xsl:apply-templates select="text"/>
        </block>
    </xsl:template>
    
    <!-- Match each source text element and create a simplified text element -->
    <xsl:template match="text">
        <text>
            <!-- Process every paragraph (par) inside the current text -->
            <xsl:apply-templates select="par"/>
        </text>
    </xsl:template>
    
    <!-- Match each source par and create a simplified paragraph element -->
    <xsl:template match="par">
        <paragraph>
            <!-- Process every line inside the current paragraph -->
            <xsl:apply-templates select="line"/>
        </paragraph>
    </xsl:template>
    
    <!-- Match each source line, create a simplified line, output the text from formatting -->
    <xsl:template match="line">
        <line>
            <xsl:value-of select="formatting"/>
        </line>
    </xsl:template>
    
    <!-- Ignore any other elements/attributes we don't need in the output -->
    <xsl:template match="*|@*[not(name()='blockType')]"/>
</xsl:stylesheet>

Why This Works

  • Template matching: Instead of hardcoding loops, we use XSL's native template matching to walk through the source XML hierarchy. This is the standard, maintainable way to write XSL transformations.
  • Relative context: Each template operates on its current element, so <xsl:apply-templates select="page"/> only selects the child pages of the current document, not all pages in the entire file.
  • Attribute value template: blockType="{@blockType}" is a clean shorthand to copy the current block's blockType attribute without verbose <xsl:attribute> code.
  • Targeted text extraction: We pull the text directly from the <formatting> element, which is where your actual content is stored in the source XML.

When you run this XSL against your source XML, it will produce exactly the simplified output you're expecting.

内容的提问来源于stack exchange,提问作者shashi sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 17:22:46