XSLT 输出到 HTML:添加一个 HTML 元素,其数字递增,基于另一个元素
XSLT output to HTML: add an HTML element with incremented number, based on another element
我有 tei:xml
个文档,我正在使用 XSLT 2.0 将其转换为 HTML。 tei 文档的有效结构如下所示:
...
<p xml:lang="LA">
<seg type="a" corresp="#fooid"><date type="doc_date" when="1245"/>In non hendrerit metus. Sed in
posuere eros, sit amet pharetra lacus.</seg>
<seg type="a">Nullam semper varius justo, vitae mollis turpis
dapibus sit amet. Donec<note type="public_note"></note>
rhoncus tempor urna sit amet
imperdiet.</seg>
<seg type="a">Integer id ante nunc. Curabitur at ligula sed
arcu consequat gravida et id orci. Morbi quis porta
dolor.</seg>
<seg type="a" corresp="#fooid2">Sed dictum<note type="public_note">
sem nec urna sodales cursus. Donec sit amet nibh tempor,
congue ligula semper, rhoncus odio.</seg>
<p>
...
在几个 <xsl:template>
中,我将 xml 转换为 HTML,然后循环浏览 tei 文档以识别需要转换为上标脚注编号的元素。我使用 <xsl:number function>
来增加数字:
<xsl:template match="p">
<div><xsl:apply-templates></div>
</xsl:template>
<xsl:template match="seg[@type='a']">
<p><xsl:apply-templates></p>
</xsl:template>
<xsl:template match="seg//date[@type='doc_date'] |
seg//note[@type='public_note']">
<sup>
<xsl:number count="seg//date[@type='doc_date'] |
seg//note[@type='public_note']" format="1" level="any"/>
</sup>
</xsl:template>
生成三个增量值 1、2、3 的 <sup/>
:
<div>
<p><sup>1</sup>In non hendrerit metus. Sed in
posuere eros, sit amet pharetra lacus.</p>
<p>Nullam semper varius justo, vitae mollis turpis
dapibus sit amet. Donec<sup>2</sup> rhoncus tempor
urna sit amet imperdiet.</p>
<p>Integer id ante nunc. Curabitur at ligula sed
arcu consequat gravida et id orci. Morbi quis porta
dolor.</p>
<p>Sed dictum sem<sup>3</sup> nec urna sodales cursus.
Donec sit amet nibh tempor, congue ligula semper,
rhoncus odio.</p>
<div>
我似乎无法解决的问题是如何输出以下内容,当
满足条件 seg[@corresp]
:
<div>
<p><sup>1</sup>In non hendrerit metus. Sed in
posuere eros, sit amet pharetra lacus.</p><sup>2</sup>
<p>Nullam semper varius justo, vitae mollis turpis
dapibus sit amet. Donec<sup>3</sup> rhoncus tempor
urna sit amet imperdiet.</p>
<p>Integer id ante nunc. Curabitur at ligula sed
arcu consequat gravida et id orci. Morbi quis porta
dolor.</p>
<p>Sed dictum sem<sup>4</sup> nec urna sodales cursus.
Donec sit amet nibh tempor, congue ligula semper,
rhoncus odio.</p><sup>5</sup>
<div>
我可以让它们在单独的模板中工作(同时创建 html <p/>
),但不能在一个模板中工作。但是,在单独的模板中会重新开始编号。
非常感谢。
您可以使用不同模式的模板来创建数字并在模式中包含 seg[@corresp]
元素(正如我在 https://xsltfiddle.liberty-development.net/pPqsHUb 中所做的那样),但是 xsl:number
工作基于在源文档中节点的位置上,您没有得到您所指示的顺序,因为基本上 seg[@corresp]
元素以较低的数字作为其子代或后代 date
或 note
元素。
所以基本上我认为你需要 运行 两步转换,在 seg[@corresp]
的末尾添加 note
或 date
或其他标记元素,然后在第二步中对它们和 note
/date
进行编号:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output method="html" indent="yes" html-version="5"/>
<xsl:mode name="add-marker" on-no-match="shallow-copy"/>
<xsl:template match="seg[@corresp]" mode="add-marker">
<xsl:next-match/>
<marker/>
</xsl:template>
<xsl:variable name="markers-added">
<xsl:apply-templates mode="add-marker"/>
</xsl:variable>
<xsl:template match="/">
<xsl:apply-templates select="$markers-added/node()"/>
</xsl:template>
<xsl:template match="p">
<div><xsl:apply-templates/></div>
</xsl:template>
<xsl:template match="seg[@type='a']">
<p><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="seg//date[@type='doc_date'] |
seg//note[@type='public_note'] | marker">
<xsl:apply-templates select="." mode="number"/>
</xsl:template>
<xsl:template match="*" mode="number">
<sup>
<xsl:number count="marker | seg//date[@type='doc_date'] |
seg//note[@type='public_note']" format="1" level="any"/>
</sup>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/pPqsHUb/1
我在其中使用了 XSLT 3 xsl:mode
声明,但它可以被身份转换模板替换,例如
<xsl:template match="@* | node()" mode="add-marker">
<xsl:copy>
<xsl:apply-templates select="@* | node()" mode="#current"/>
</xsl:copy>
</xsl:template>
对于 XSLT 2 处理器。
我有 tei:xml
个文档,我正在使用 XSLT 2.0 将其转换为 HTML。 tei 文档的有效结构如下所示:
...
<p xml:lang="LA">
<seg type="a" corresp="#fooid"><date type="doc_date" when="1245"/>In non hendrerit metus. Sed in
posuere eros, sit amet pharetra lacus.</seg>
<seg type="a">Nullam semper varius justo, vitae mollis turpis
dapibus sit amet. Donec<note type="public_note"></note>
rhoncus tempor urna sit amet
imperdiet.</seg>
<seg type="a">Integer id ante nunc. Curabitur at ligula sed
arcu consequat gravida et id orci. Morbi quis porta
dolor.</seg>
<seg type="a" corresp="#fooid2">Sed dictum<note type="public_note">
sem nec urna sodales cursus. Donec sit amet nibh tempor,
congue ligula semper, rhoncus odio.</seg>
<p>
...
在几个 <xsl:template>
中,我将 xml 转换为 HTML,然后循环浏览 tei 文档以识别需要转换为上标脚注编号的元素。我使用 <xsl:number function>
来增加数字:
<xsl:template match="p">
<div><xsl:apply-templates></div>
</xsl:template>
<xsl:template match="seg[@type='a']">
<p><xsl:apply-templates></p>
</xsl:template>
<xsl:template match="seg//date[@type='doc_date'] |
seg//note[@type='public_note']">
<sup>
<xsl:number count="seg//date[@type='doc_date'] |
seg//note[@type='public_note']" format="1" level="any"/>
</sup>
</xsl:template>
生成三个增量值 1、2、3 的 <sup/>
:
<div>
<p><sup>1</sup>In non hendrerit metus. Sed in
posuere eros, sit amet pharetra lacus.</p>
<p>Nullam semper varius justo, vitae mollis turpis
dapibus sit amet. Donec<sup>2</sup> rhoncus tempor
urna sit amet imperdiet.</p>
<p>Integer id ante nunc. Curabitur at ligula sed
arcu consequat gravida et id orci. Morbi quis porta
dolor.</p>
<p>Sed dictum sem<sup>3</sup> nec urna sodales cursus.
Donec sit amet nibh tempor, congue ligula semper,
rhoncus odio.</p>
<div>
我似乎无法解决的问题是如何输出以下内容,当
满足条件 seg[@corresp]
:
<div>
<p><sup>1</sup>In non hendrerit metus. Sed in
posuere eros, sit amet pharetra lacus.</p><sup>2</sup>
<p>Nullam semper varius justo, vitae mollis turpis
dapibus sit amet. Donec<sup>3</sup> rhoncus tempor
urna sit amet imperdiet.</p>
<p>Integer id ante nunc. Curabitur at ligula sed
arcu consequat gravida et id orci. Morbi quis porta
dolor.</p>
<p>Sed dictum sem<sup>4</sup> nec urna sodales cursus.
Donec sit amet nibh tempor, congue ligula semper,
rhoncus odio.</p><sup>5</sup>
<div>
我可以让它们在单独的模板中工作(同时创建 html <p/>
),但不能在一个模板中工作。但是,在单独的模板中会重新开始编号。
非常感谢。
您可以使用不同模式的模板来创建数字并在模式中包含 seg[@corresp]
元素(正如我在 https://xsltfiddle.liberty-development.net/pPqsHUb 中所做的那样),但是 xsl:number
工作基于在源文档中节点的位置上,您没有得到您所指示的顺序,因为基本上 seg[@corresp]
元素以较低的数字作为其子代或后代 date
或 note
元素。
所以基本上我认为你需要 运行 两步转换,在 seg[@corresp]
的末尾添加 note
或 date
或其他标记元素,然后在第二步中对它们和 note
/date
进行编号:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output method="html" indent="yes" html-version="5"/>
<xsl:mode name="add-marker" on-no-match="shallow-copy"/>
<xsl:template match="seg[@corresp]" mode="add-marker">
<xsl:next-match/>
<marker/>
</xsl:template>
<xsl:variable name="markers-added">
<xsl:apply-templates mode="add-marker"/>
</xsl:variable>
<xsl:template match="/">
<xsl:apply-templates select="$markers-added/node()"/>
</xsl:template>
<xsl:template match="p">
<div><xsl:apply-templates/></div>
</xsl:template>
<xsl:template match="seg[@type='a']">
<p><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="seg//date[@type='doc_date'] |
seg//note[@type='public_note'] | marker">
<xsl:apply-templates select="." mode="number"/>
</xsl:template>
<xsl:template match="*" mode="number">
<sup>
<xsl:number count="marker | seg//date[@type='doc_date'] |
seg//note[@type='public_note']" format="1" level="any"/>
</sup>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/pPqsHUb/1
我在其中使用了 XSLT 3 xsl:mode
声明,但它可以被身份转换模板替换,例如
<xsl:template match="@* | node()" mode="add-marker">
<xsl:copy>
<xsl:apply-templates select="@* | node()" mode="#current"/>
</xsl:copy>
</xsl:template>
对于 XSLT 2 处理器。