使用 XSL 将复杂的 XML 转换为简化的 XML

Translating complex XML to simplified XML using XSL

我制作了一个 XSL 文件来将 src.xml 转换为 target.xml。但是我没有得到正确的预期输出文件。

这是我的代码:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<document xmlns="http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml" version="1.0" producer="ABBYY FineReader Engine 12" languages="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml">
<page width="294" height="189" resolution="120" originalCoords="1">
<block blockType="Text" blockName="" l="0" t="5" r="272" b="185"><region><rect l="0" t="5" r="272" b="185"/></region>
<text>
<par lineSpacing="2410">
<line baseline="30" l="1" t="6" r="72" b="30"><formatting lang="EnglishUnitedStates">hello</formatting></line></par>
<par lineSpacing="1840">
<line baseline="87" l="0" t="69" r="179" b="87"><formatting lang="EnglishUnitedStates">this is a website</formatting></line></par>
<par lineSpacing="1260">
<line baseline="136" l="0" t="122" r="269" b="140"><formatting lang="EnglishUnitedStates">Is the writing getting smaller?</formatting></line></par>
<par lineSpacing="1260">
<line baseline="182" l="0" t="169" r="133" b="182"><formatting lang="EnglishUnitedStates">IM SHRINKING</formatting></line></par>
</text>
</block>
</page>
</document>

现在这是将复杂 xml 转换为普通 xml 的 XSL:

 <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"  xmlns="http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
        <xsl:template match="/">
            <document>
                <page>
                    <block>
                        <xsl:variable name="blockType" select="/document/page/block/@blockType"/>
                        <!-- The variable blockType can be used for further processing.  -->
                        
                        
                        <xsl:attribute name="blockType"><xsl:value-of select="$blockType"/></xsl:attribute>
                        <text>
                            <xsl:for-each select="/document/page/block/text/par">
                                <paragraph>
                                    <line>
                                        <xsl:value-of   select="./line"/>
                                    </line>
                                </paragraph>
                            </xsl:for-each>
                        </text>
                    </block>
                </page>
            </document>
        </xsl:template>
    </xsl:stylesheet>

实际输出

<?xml version="1.0" encoding="UTF-8"?><document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml">
    <page>
        <block blockType="">
            <text/>
        </block>
    </page>
</document>

我想要的预期输出:

<?xml version="1.0" encoding="UTF-8"?>
<document>
    <page>
        <block blockType="Text">
            <text>
                <paragraph>
                    <line>hello</line>
                </paragraph>
                <paragraph>
                    <line>this is a website</line>
                </paragraph>
                <paragraph>
                    <line>Is the writing getting smaller?</line>
                </paragraph>
                <paragraph>
                    <line>IM SHRINKING</line>
                </paragraph>
            </text>
        </block>
    </page>
</document>

您可以将 xpath-default-namespace 用于 XSLT:2.0

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  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"/>
    <xsl:template match="/">
        <document>
            <page>
                <block>
                    <xsl:variable name="blockType" select="/document/page/block/@blockType"/>
                    <!-- The variable blockType can be used for further processing.  -->
                    
                    
                    <xsl:attribute name="blockType"><xsl:value-of select="$blockType"/></xsl:attribute>
                    <text>
                        <xsl:for-each select="/document/page/block/text/par">
                            <paragraph>
                                <line>
                                    <xsl:value-of   select="./line"/>
                                </line>
                            </paragraph>
                        </xsl:for-each>
                    </text>
                </block>
            </page>
        </document>
    </xsl:template>
</xsl:stylesheet>

检查:http://xsltfiddle.liberty-development.net/gWmuiHX/30