首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用XSL将复杂XML转换为简化XML

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

Stack Overflow用户
提问于 2021-10-08 10:30:55
回答 1查看 109关注 0票数 0

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

这是我的密码:

代码语言:javascript
运行
复制
<?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:

代码语言:javascript
运行
复制
 <?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>

实际输出

代码语言:javascript
运行
复制
<?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>

我想要的预期输出:

代码语言:javascript
运行
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-08 10:56:12

您可以使用xpath-default-namespace for XSLT:2.0

代码语言:javascript
运行
复制
    <?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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69494429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档