首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将.tei文件转换为.txt文件

将.tei文件转换为.txt文件
EN

Stack Overflow用户
提问于 2018-10-05 21:14:46
回答 1查看 486关注 0票数 0

我有以下格式的.tei文件。

代码语言:javascript
复制
<biblStruct xml:id="b0">
    <analytic>
        <title level="a" type="main">The Semantic Web</title>
        <author>
            <persName xmlns="http://www.tei-c.org/ns/1.0">
                <forename type="first">T</forename>
                <surname>Berners-Lee</surname>
            </persName>
        </author>
        <author>
            <persName xmlns="http://www.tei-c.org/ns/1.0">
                <forename type="first">J</forename>
                <surname>Hendler</surname>
            </persName>
        </author>
        <author>
            <persName xmlns="http://www.tei-c.org/ns/1.0">
                <forename type="first">O</forename>
                <surname>Lassilia</surname>
            </persName>
        </author>
    </analytic>
    <monogr>
        <title level="j">Scientific American</title>
        <imprint>
            <date type="published" when="2001-05" />
        </imprint>
    </monogr>
</biblStruct>

我想将上面的文件转换成.txt格式,如下所示:

书名/责任者: Hendler and J. Lassilia.“语义网”,科学美国人,2001年5月

我尝试使用以下代码:

代码语言:javascript
复制
tree = ET.parse(path)
root = tree.getroot()
s = ""
for childs in root:
    for child in childs:
        s= s+child.text

上述代码的问题是循环按顺序执行,而字符串不是顺序格式的。

其次,可能会有更多的内部循环。在不手动检查的情况下提取内部循环中的内容也是有问题的。请帮我处理这个

EN

回答 1

Stack Overflow用户

发布于 2018-10-05 22:39:27

我知道您正在寻找Python解决方案,但是由于XSLT是一种非常方便的替代方法,而且非常适合.xml文件,所以无论如何我都会发布一个XSLT解决方案。

我想它可以很容易地集成到Python解决方案中。

因此,这是必要的XSLT:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tei="http://www.tei-c.org/ns/1.0" xmlns:month="http://month.com">
    <xsl:output method="text" />
    <xsl:strip-space elements="*" />

    <month:month>
        <month name="Jan" />
        <month name="Feb" />
        <month name="Mar" />
        <month name="Apr" />
        <month name="May" />
        <month name="Jun" />
        <month name="Jul" />
        <month name="Aug" />
        <month name="Sep" />
        <month name="Oct" />
        <month name="Nov" />
        <month name="Dec" />
    </month:month>

    <xsl:template match="author[position()=1]">
        <xsl:value-of select="concat(tei:persName/tei:forename, '. ',tei:persName/tei:surname)" />
    </xsl:template>    

    <xsl:template match="author">
        <xsl:value-of select="concat(', ',tei:persName/tei:forename, '. ',tei:persName/tei:surname)" />
    </xsl:template>

    <xsl:template match="author[last()]">
        <xsl:value-of select="concat(' and ',tei:persName/tei:forename, '. ',tei:persName/tei:surname)" />
    </xsl:template>

    <xsl:template match="/biblStruct">
        <xsl:apply-templates select="analytic/author" />
        <xsl:variable name="mon" select="number(substring(monogr/imprint/date/@when,6,2))" />
        <xsl:value-of select='concat(" &apos;",analytic/title,"&apos;",", ",monogr/title, ", ")' />   
        <xsl:value-of select="document('')/xsl:stylesheet/month:month/month[$mon]/@name" />
        <xsl:value-of select="concat(' ',/xsl:stylesheet/month:month[substring(monogr/imprint/date/@when,5,2)],substring(monogr/imprint/date/@when,1,4))" />
    </xsl:template>

</xsl:stylesheet>

要理解这段代码,您不需要了解多少XSLT:

有三个模板匹配author元素--一个匹配第一个匹配,一个匹配last()匹配,另一个匹配两者之间的匹配。它们仅在处理像,and这样的分隔符方面有所不同。

最后一个模板处理整个XML并组合其他三个模板的输出。它还通过引用month:month数据岛将数字月份号转换为字符串。

还应该查看xsl:stylesheet元素的定义名称空间:

  • 一个用于XSL:http://www.w3.org/1999/XSL/Transform
  • TEI的一个:http://www.tei-c.org/ns/1.0
  • 每月一次:数据岛的http://month.com

我希望我已经为使用XSLT文件进行转换提供了令人信服的理由。xsl:output元素确实使用method="text"指定了所需的文本输出目标。

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

https://stackoverflow.com/questions/52673360

复制
相关文章

相似问题

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