1

我正在编写具有这样基本结构的 TEI 文档。一章中有几个“mainText”部分;这些部分具有实际文本的单独规范化和 OCR 版本。

<div type="chapter">
    <div type="mainText">
        <div type="normalized">
             <p>HERE COMES <lb/> SOME TEXT<lb/></p>
        </div>
        <div type="OCR">
             <p>HERE COMES <lb/> SOME TEXT<lb/></p>
        </div>
    </div>
    <div type="mainText">
        <div type="normalized">
             <p>HERE COMES <lb/> SOME TEXT<lb/></p>
        </div>
        <div type="OCR">
             <p>HERE COMES <lb/> SOME TEXT<lb/></p>
        </div>
    </div>
</div>

使用 XSLT 2.0,我现在正在尝试执行以下步骤,这些步骤已经有效:

  • 将每章中的 mainText-divs 替换为<ab/>
  • 将规范化和 OCR 版本替换为元素<reg/><orig/>
  • 替换<p>为线组元素<lg>
  • 在 linegroup 内部,将每个<lb/>以 line 元素结尾的组包装起来<l/>

我的问题如下:我想为每一行分配一个行号属性,但是在章节级别,意思是:在一个章节中有一个连续的行计数器。查看我当前使用的 xsl 模板:

<!-- replace p with linegroup -->
    <xsl:template match="text//p">
        <xsl:choose>

            <!-- don't apply lingroup when there is nothing inside of p -->
            <xsl:when test="not(node())">
                <xsl:apply-templates/>
            </xsl:when>

            <xsl:otherwise>
                <lg>
                    <!-- make a group out of everything inside of p, ending with a linebreak -->
                    <xsl:for-each-group select="node()" group-ending-with="lb">

                        <!-- wrap a line aroung current group -->
                        <l>
                            <!-- for line element create number, if line is in mainText -->
                            <xsl:attribute name="n">
                                <xsl:number/>
                            </xsl:attribute>
                            <xsl:apply-templates select="current-group()"/>
                        </l>
                    </xsl:for-each-group>
                </lg>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- get rid if linebreak, as we don't need it anymore -->
    <xsl:template match="p//lb"/>

其输出将创建行号,但在每个 mainText 元素中开始计数。会很高兴得到帮助。

最好的,多米尼卡

4

1 回答 1

0

尝试这个:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- remove disallowed elements but keep its children -->
    <xsl:template match="div">
        <xsl:element name="{if(@type='mainText') then 'ab' else
            if(@type='normalized') then 'reg' else
            if(@type='OCR') then 'orig' else 'div'}">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="p//lb"/>
    <xsl:template match="p">
        <lg>
            <xsl:choose>
                <xsl:when test="not(node())">
                    <xsl:apply-templates/>
                </xsl:when>
                <xsl:otherwise>

                    <xsl:for-each-group select="node()" group-ending-with="lb">

                        <!-- wrap a line aroung current group -->
                        <l>
                            <!-- for line element create number, if line is in mainText -->
                            <xsl:attribute name="n">
                                <xsl:variable name="num">
                                <xsl:number count="lb" level="any"/>
                                </xsl:variable>
                                <xsl:value-of select="if ($num = '') then 1 else number($num) + 1"/>
                            </xsl:attribute>
                            <xsl:apply-templates select="current-group()"/>
                        </l>
                    </xsl:for-each-group>
                </xsl:otherwise>
            </xsl:choose>
        </lg>
    </xsl:template>
</xsl:stylesheet>

请参阅http://xsltransform.net/ei5Pwip上的转换

于 2017-12-22T18:15:57.720 回答