首页 > 解决方案 > 如何为以下 TEI 片段生成 XSLT 样式表?

问题描述

我想请你帮忙。我对 XSLT 完全陌生,我想知道是否有人可以向我展示以下 TEI 片段的正确 XSLT 样式表:

<div>
    <head>Weitere Aufzählungen</head>
    <list rend="numbered">
        <item n="1">A</item>
        <item n="2">B</item>                           
        <item n="3">C<list rend="numbered">
            <item n="3.1">a</item>
            <item n="3.2">b</item>
            </list>
        </item>
    </list>
</div>

输出应类似于 HTML 文档中的内容:

1. A
2. B
3. C
    3.1 a
    3.2 b

非常感谢你帮助我:)

标签: xslttei

解决方案


如果你想要一个文本输出,下面的样式表<xsl:output method="text" />就可以了。.它通过计算项目祖先节点来区分缩进级别,并在级别 0 上添加额外的。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes" />
  <xsl:variable name="newLine"  select="'&#xa;'" />

    <xsl:template match="text()" />

    <xsl:template match="/div/list">
        <xsl:apply-templates select="item" />
    </xsl:template>

    <xsl:template match="item">
        <xsl:for-each select="ancestor::item"><xsl:text>   </xsl:text></xsl:for-each>
        <xsl:value-of select="@n" />
        <xsl:if test="not(ancestor::item)"><xsl:text>.</xsl:text></xsl:if>
        <xsl:value-of select="concat(' ',text(),$newLine)" />
        <xsl:apply-templates select="list" />
    </xsl:template>   
</xsl:stylesheet>

输出是:

1. A
2. B
3. C
   3.1 a
   3.2 b

顺便说一句,您可能需要将 TEI 命名空间的适当命名空间声明添加到xsl:stylesheet元素。


推荐阅读