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

时间:2018-10-05 21:14:46

标签: python xml tei

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

<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格式,如下所示:

  

T。 Berners-Lee,J.Hendler和O.Lassilia。 “语义网”,《科学美国人》,2001年5月

我尝试使用以下代码:

tree = ET.parse(path)
root = tree.getroot()
s = ""
for childs in root:
    for child in childs:
        s= s+child.text

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

第二,可能会有更多的内部循环。在不手动检查的情况下提取内部循环中的内容也是有问题的。请帮助我

1 个答案:

答案 0 :(得分:0)

我知道您正在寻找Python解决方案,但是由于XSLT是一种便捷的选择,并且非常适合.xml文件,因此无论如何我都会发布XSLT解决方案。

我想它可以轻松集成到您的Python解决方案中。
因此,这是必需的XSLT:

<?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"指定了所需的文本输出目标。