我目前正在为the (https://tei-c.org/release/doc/tei-xsl/)开发一个配置文件,以收集从MSword docx格式到TEI符合XML的转换(以及进一步到有效的MSword)。在我的例子中,我需要定制的一个特定的转换是,我有一堆文本,这些文本引用了特定的视频源存档。在文本中,这些拒绝就像方框: 001,滚动: 01,开始: 00:01:00.00。我想使用regex查找这些引用,并在TEI :图元素中生成一个tei一致性tei:media元素。当引用在它自己的段落中时,这是很好的。但是不同的作者在他们的文本段落中都有引用(元素tei:p)。这里开始了挑战,因为这些祈祷图可能包含其他元素,如tei:note或tei:hi,应该是完整的,并且处理得很好。不幸的是,xslt指令xsl:analyze string创建子字符串,因此不能对其使用xsl:apply-模板,而只能使用xsl:copy-of。这适用于xsl:matching substring,但是xsl:non substring包含前面提到的一些其他元素(带有属性),这些元素应该被处理。
TEI样式表转换相当复杂,并运行各种传递。在这个阶段,我想干预我的个人资料,我已经有一个tei元素p为我的段落。例如:
<p>This is my paragraph with a note <note place="foot">This is my note</note> and it is <hi rend="italic">important</hi> that this inline elements and their attributes are kept and further processed. This is my special reference to a video in the archive [box: 001 roll: 01 start: 00:01:10.12] that should be transformed into a valid tei:media element.</p>到目前为止,我的转换(简化):
<xsl:template match="tei:p" mode="pass2">
<xsl:choose>
<xsl:when test=".,'\[[Bb]ox:.+?\]'">
<xsl:analyze-string select="." regex="\[box: (\d+) roll: (\d+) start: ((\d\d):(\d\d):(\d\d).(\d\d))\]">
<xsl:matching-substring>
<xsl:element name="ref">
<xsl:attribute name="target">
<xsl:value-of select="concat('https://path-to-video-page/',regex-group(1),'-',regex-group(2),'/',regex-group(4),'-'regex-group(5),'-',regex-group(6),'-',regex-group(7))"/>
</xsl:attribute>
<xsl:value-of select="concat('(box: ',regex-group(1),' roll: ',regex-group(2),' @ ',regex-group(4),'h 'regex-group(5),'m ',regex-group(6),'s)')"/>
</xsl:element>
<figure place="margin">
<xsl:element name="head">
<xsl:value-of select="concat('Sequence from box: ',regex-group(1),' roll: ',regex-group(2))"/>
</xsl:element>
<xsl:element name="media">
<xsl:attribute name="mimeType">video/mp4</xsl:attribute>
<xsl:attribute name="url">
<xsl:value-of select="concat('https://path-to-video/',regex-group(1),'-',regex-group(2),'.mp4')"/>
</xsl:attribute>
<xsl:attribute name="start">
<xsl:value-of select="regex-group(3)"/>
</xsl:attribute>
</xsl:element>
</figure>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:copy-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
<xsl:otherwise>
<xsl:apply-templates mode="pass2"/>
</xsl:otherwise>
</xsl:choose>
</p>
</xsl:template>在以下方面的成果:
<p>This is my paragraph with a note This is my note and it is important that this inline elements and their attributes are kept and further processed. This is my special reference to a video in the archive <ref target="https://path-to-video-page/001-01/00-01-10-12">(box: 001 roll: 01 @ 00h 01m 10s)</ref>
<figure rend="margin">
<head rend="none">Sequence from box: 001 roll: 01</head>
<media mimeType="video/mp4" url="path-to-video/001-01.mp4" start="00:01:10.12"/>
</figure> that should be transformed into a valid tei:media element.</p>现在我被困住了。是否可以使用regex操作p元素中文本的匹配内容,同时维护非匹配部分的“节点字符”以供进一步处理?或者,我是处于死胡同,应该停止为此目的与xml混在一起吗?我正在考虑的另一种方法是将引用保留为xml中的文本,并使用python对结果的xml/html文件进行后处理。但是,如果可能的话,用xslt来完成所有事情将更加优雅。
谢谢你的建议,奥拉夫
发布于 2022-05-24 21:23:45
解决方案非常简单:将模板匹配更改为
xsl:template match="tei:p//text()"当应用于tei:p时,xsl:analyze-string将整个元素分解为可以用regex解析的字符串。只匹配文本节点tei:p//text()保留tei:p及其父/祖先/同级元素的其余元素结构。然后,xsl:analyze-string只对文本进行操作,并保持其余部分由其他模板或默认标识转换处理。
xsl:analyze-string的许多教程或示例将其应用于整个元素,因为他们只想提取一些信息以供进一步处理,而将原始元素抛在后面。如果要使用xsl:analyze-string更改进一步用作元素的元素的文本,则必须只将其应用于文本节点。
感谢@Martin Honnen在对我的问题的评论中给出了这个建议。
https://stackoverflow.com/questions/72364291
复制相似问题