我正在處理 XML-TEI 中的一些手稿轉錄,并且正在使用 XSLT 將其轉換為 .tex 檔案。我的輸入檔案由tei:w代表文本中每個單詞的標記組成。MWE:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="http://www.tei-c.org/release/xml/tei/custom/schema/relaxng/tei_all.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1.0"?>
<?xml-model href="http://www.tei-c.org/release/xml/tei/custom/schema/relaxng/tei_all.rng" type="application/xml"
schematypens="http://purl.oclc.org/dsdl/schematron"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader>
...
</teiHeader>
<text>
<body>
<p><w>Lorem</w>
<w>ipsum</w>
<w>dolor</w>
<w>sit</w>
<w>amet</w>
<pc>,</pc>
<w>consectetur</w>
<w>adipiscing</w>
<w>elit</w>
<pc>,</pc>
<w>sed</w>
<w>do</w>
<w>eiusmod</w>
<w>tempor</w>
<w>incididunt</w>
<w>ut</w>
<w>labore</w>
<w>et</w>
<w>dolore</w>
<w>magna</w>
<w>aliqua</w>
<pc>;</pc>
<w>ut</w>
<w>enim</w>
<w>ad</w>
<w>minim</w>
<w>veniam</w>
</p>
</body>
</text>
</TEI>
我需要識別在一定范圍內重復的單詞,比如 10,以使 LaTeX 在版本中消除它們的歧義(使用\sameword從reledmac包呼叫的命令)。例如,在上面的 MWE 中,我希望兩者都ut被這個命令標記。
我想我已經找到了一種方法來做到這一點;我的問題更多是關于如何改進我的代碼。對于小檔案,下面的模板似乎作業得很好;但是我的語料庫由 300.000 個標記組成,并且轉換花費了太多時間:引擎正在評估每個單詞的左右背景關系......
<xsl:template match="tei:w">
<xsl:variable name="current_position" select="count(preceding::tei:w)"/>
<xsl:variable name="same_word_before"
select="preceding::tei:w[($current_position - 10) > count(preceding::tei:w)][not(count(preceding::tei:w) > $current_position)]/text() = text()"/>
<xsl:variable name="same_word_after"
select="following::tei:w[($current_position 10) > count(preceding::tei:w)][count(preceding::tei:w) > $current_position]/text() = text()"/>
...
<xsl:choose>
<xsl:when test="$same_word_before or $same_word_after">
<xsl:text>\sameword{</xsl:text>
<xsl:apply-templates/>
<xsl:text>}</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
...
</xsl:template>
有沒有更簡單和/或更有效的方法來做到這一點?我正在考慮的一個解決方案是使用 python,但我更愿意堅持使用 xsl 來完成這項任務。
編輯:我正在使用 XSLT 2.0。
uj5u.com熱心網友回復:
與您所做的沒有太大區別,仍然很快:
<xsl:template match="tei:w">
<xsl:variable name="preceding" as="xs:string*" select="preceding-sibling::tei:w[position() lt 11]/text()" />
<xsl:variable name="following" as="xs:string*" select="following-sibling::tei:w[position() lt 11]/text()" />
<xsl:choose>
<xsl:when test="text()=($preceding,$following)">
<xsl:text>\sameword{</xsl:text>
<xsl:apply-templates/>
<xsl:text>}</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
我用每 50 個單詞用 2000 個 p 對其進行了測驗,耗時 0.3 秒。
從 Xslt 2.0 開始,我們有內置的資料型別 它們描述了變數/引數/函式的資料型別。
即
<xsl:variable name="preceding" as="xs:string*"/>意味著變數可以包含零個或多個字串。或
<xsl:variable name="firtsNextSibling" as="element()?"/>表示變數可以包含零個或一個元素。
<xsl:when test="text()=($preceding,$following)">
this when 的 @test 屬性的含義是當前 text()-node 的值應該存在于組合的 $preceding 和 $following 字串序列中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/436291.html
上一篇:XSLT-參考HREF元素
