【问题标题】:Converting .tei file to a .txt file将 .tei 文件转换为 .txt 文件
【发布时间】:2018-10-05 21:14:46
【问题描述】:

我有一个以下格式的.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

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

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

【问题讨论】:

  • 仅供参考:将 XML 文件转换为 TXT 文件的最简单方法是 XSLT。

标签: python xml tei


【解决方案1】:

我知道您正在寻找 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" 指定了所需的文本输出目标。

【讨论】:

    猜你喜欢
    • 2019-08-29
    • 2016-10-02
    • 2015-12-11
    • 2020-01-08
    • 1970-01-01
    • 2021-12-24
    • 2017-02-09
    • 2017-09-23
    • 2014-06-11
    相关资源
    最近更新 更多