我有一个使用 TEI 构建的 XML 文件,如下所示:
<div type="chapter" n="1">
<p>
<s xml:id="e_1">sentence e1.</s>
<s xml:id="f_1">sentence f1</s>
</p>
<p>
<s xml:id="e_2"> sentence e2</s>
<s xml:id="f_2"> sentence f2</s>
</p>
</div>
<div type="chapter" n="2">
<!-- -->
</div>
我需要将其转换为这种 HTML 结构:
<div>
<h1>Chapter 1</h1>
<div class="book-content">
<p>
<span class='source-language-sent' data-source-id='1'>sentence e1.</span>
<span id='1' style='display:none'>sentence f1</span>
</p>
<p>
<span class='source-language-sent' data-source-id='2'>sentence e2</span>
<span id='2' style='display:none'>sentence f2</span>
</p>
</div>
</div>
<div>
<h1>Chapter 2</h1>
<div class="book-content">
<!-- -->
</div>
</div>
现在我使用这个 XSLT 文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tei="http://www.tei-c.org/ns/1.0" version="1.0">
<xsl:output method="html" encoding="UTF-8" indent="yes" />
<xsl:template match="tei:body">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="tei:teiHeader">
<xsl:comment>
<xsl:apply-templates select="node()" />
</xsl:comment>
</xsl:template>
<!--create chapter-->
<xsl:template match="tei:div">
<xsl:element name="div">
<xsl:element name="div">
<xsl:attribute name="class">
<xsl:text>book-content</xsl:text>
</xsl:attribute>
<xsl:element name="h1">
<xsl:text>Chapter</xsl:text>
<xsl:value-of select="@n" />
</xsl:element>
<xsl:apply-templates select="node()" />
</xsl:element>
</xsl:element>
</xsl:template>
<!-- create p-->
<xsl:template match="tei:p">
<xsl:element name="p">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- create s-->
<xsl:template match="tei:s">
<xsl:variable name="xmlid" select="@xml:id" />
<xsl:if test="starts-with($xmlid, 'e')">
<xsl:element name="span">
<xsl:attribute name="class">
<xsl:text>source-language-sent</xsl:text>
</xsl:attribute>
<xsl:attribute name="data-source-id">
<xsl:value-of select="substring($xmlid, 3, 4)" />
</xsl:attribute>
<xsl:apply-templates select="node()" />
</xsl:element>
</xsl:if>
<xsl:if test="starts-with($xmlid, 'f')">
<xsl:element name="span">
<xsl:attribute name="style">
<xsl:text>display:none</xsl:text>
</xsl:attribute>
<xsl:attribute name="id">
<xsl:value-of select="substring($xmlid, 3, 4)" />
</xsl:attribute>
<xsl:apply-templates select="node()" />
</xsl:element>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
我的问题是我需要创建一个新的<div class="book-content">
foreach 900 个字符。但我不想削减我的元素,所以我需要计算我必须在一个元素中包含s
多少个元素才能拥有大约 900 个字符。s
<div class="book-content">