TEI XML转HTML的XSL样式表问题求助:无格式输出及排版需求
TEI转HTML的XSL样式表修正方案
问题简述
我正在编写XSL样式表,将TEI格式XML转换为可发布的HTML。目标需求:
- 把
<label>内容设置为加粗样式 - 将
section类型的<milestone>(如11b)放置在文本左侧边距
但当前编写的XSL输出的是无换行、无加粗的纯文本,求解决思路。
示例XML
<?xml version="1.0" encoding="UTF-8"?> <?xml-model href="http://www.stoa.org/epidoc/schema/latest/tei-epidoc.rng" schematypens="http://relaxng.org/ns/structure/1.0"?> <TEI xmlns="http://www.tei-c.org/ns/1.0"> <teiHeader> ... </teiHeader> <text xml:lang="grc"> <body> <div type="edition" n="urn:cts:greekLit:tlg0059.tlg010.perseus-grc2" xml:lang="grc"> <div type="textpart" subtype="section" resp="perseus" n="11"> <p> <said who="#Σωκράτης"> <milestone unit="page" resp="Stephanus" n="11"/> <milestone unit="section" resp="Stephanus" n="11a"/> <label>ΣΩ.</label> ὅρα δή, Πρώταρχε, τίνα λόγον μέλλεις παρὰ Φιλήβου δέχεσθαι νυνὶ καὶ πρὸς τίνα τὸν παρʼ ἡμῖν ἀμφισβητεῖν, <milestone unit="section" resp="Stephanus" n="11b"/> ἐὰν μή σοι κατὰ νοῦν ᾖ λεγόμενος. βούλει συγκεφαλαιωσώμεθα ἑκάτερον; </said></p></div></div></body></text> </TEI>
原XSL样式表
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/HTML" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <xsl:apply-templates/> </xsl:template> <xsl:template match="label"> <b> <xsl:apply-templates/> </b> </xsl:template> <xsl:template match="p"> <p> <xsl:apply-templates/> </p> </xsl:template> </xsl:stylesheet>
核心问题分析
你的XML中所有TEI元素都属于http://www.tei-c.org/ns/1.0命名空间,但原XSL没有声明该命名空间的前缀,导致模板完全匹配不到任何TEI元素,只能输出XML中的纯文本节点,自然没有加粗和段落结构。
修正后的XSL样式表
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:html="http://www.w3.org/1999/HTML" xmlns:tei="http://www.tei-c.org/ns/1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" encoding="UTF-8" indent="yes"/> <!-- 跳过teiHeader,不输出到HTML中 --> <xsl:template match="tei:teiHeader"/> <!-- 生成完整的HTML结构,从body开始处理内容 --> <xsl:template match="/"> <html:html> <html:body> <xsl:apply-templates select="tei:TEI/tei:text/tei:body"/> </html:body> </html:html> </xsl:template> <!-- 匹配label元素,转为HTML加粗标签 --> <xsl:template match="tei:label"> <html:b> <xsl:apply-templates/> </html:b> </xsl:template> <!-- 保留段落结构,给段落留左侧边距放milestone --> <xsl:template match="tei:p"> <html:p style="margin-left:2em;"> <xsl:apply-templates/> </html:p> </xsl:template> <!-- 处理section类型的milestone,放在左侧边距 --> <xsl:template match="tei:milestone[@unit='section']"> <html:span style="float:left; margin-left:-2em; font-weight:bold; margin-right:0.5em;"> <xsl:value-of select="@n"/> </html:span> </xsl:template> <!-- 隐藏page类型的milestone,不需要显示 --> <xsl:template match="tei:milestone[@unit='page']"/> <!-- 直接处理said和div元素的内部内容 --> <xsl:template match="tei:said | tei:div"> <xsl:apply-templates/> </xsl:template> </xsl:stylesheet>
关键修改点
- 命名空间处理:添加
xmlns:tei="http://www.tei-c.org/ns/1.0"前缀,所有TEI元素的匹配都加上tei:,确保模板能命中目标元素;用html:前缀区分HTML元素,避免命名冲突。 - Milestone布局:给段落设置
margin-left:2em预留空间,section类型的milestone用float:left和负外边距margin-left:-2em定位到左侧边距,同时加粗突出。 - 结构优化:跳过
teiHeader,生成完整的<html>+<body>结构,确保输出的HTML符合规范;处理said和div元素,保证内容正常流转。
最终效果
<label>内容会被加粗显示- section类型的milestone(如11a、11b)会固定在段落左侧边距,不干扰正文排版
- 段落保留换行和结构,输出规范的HTML格式
内容的提问来源于stack exchange,提问作者LocusClassicus
相关产品推荐
相关产品推荐

