计算祖先章级别创建的行

问题描述 投票:1回答: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,我现在正在尝试执行以下步骤,这些步骤已经有效:

  • <ab/>替换每章内的mainText-div
  • 用元素<reg/><orig/>替换规范化和OCR版本
  • 用线组元素<p>替换<lg>
  • 在linegroup内部,将每个以<lb/>结尾的组包裹在一个行元素<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元素内开始计数。很乐意帮忙。

最好的,多米尼卡

xml xpath xslt-2.0 tei
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的转型

© www.soinside.com 2019 - 2024. All rights reserved.