XSLT 输出到 HTML:基于另一个元素添加一个带有递增编号的 HTML 元素 [英] XSLT output to HTML: add an HTML element with incremented number, based on another element

查看:28
本文介绍了XSLT 输出到 HTML:基于另一个元素添加一个带有递增编号的 HTML 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 tei:xml 文档,我正在使用 XSLT 2.0 将其转换为 HTML.tei 文档的有效结构如下所示:

<代码>...<p xml:lang="LA"><seg type="a" corresp="#foooid"><date type="doc_date" when="1245"/>在非亨德瑞特中.塞入posuere eros,坐 amet pharetra lacus.</seg><seg type="a">Nullam semper varius justo, vitae mollis turpisdapibus 坐在 amet.Donecrhoncus tempor urna 坐 amet</seg> 整数 id ante nunc.Curabitur at ligula sedarcu consequat gravida et id orci.莫尔比之门多洛尔.<seg type="a" corresp="#foooid2">Sed 格言<note type="public_note">sem nec urna sodales cursus.Donec 坐 amet nibh tempor,congue ligula semper, rhoncus odio.</seg><p>...

在几个 中,我将 xml 转换为 HTML,然后循环遍历 tei 文档以识别需要转换为上标脚注编号的元素.我使用 来增加数字:

 <div><xsl:apply-templates></div></xsl:模板><xsl:template match="seg[@type='a']"><p><xsl:apply-templates></p></xsl:模板><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:模板>

产生三个,增量值为1、2、3:

<p><sup>1</sup>在非亨德雷特米图斯中.塞入posuere eros,坐在 amet pharetra lacus.</p><p>Nullam semper varius justo, vitae mollis turpisdapibus 坐在 amet.Donec<sup>2</sup>颞肌</p><p>整数 id ante nunc.Curabitur at ligula sedarcu consequat gravida et id orci.莫尔比之门多洛尔.</p><p>Sed dictum sem<sup>3</sup>nec urna sodales cursus.Donec sat amet nibh tempor, congue ligula semper,</p><div>

我似乎无法解决的问题是如何输出以下内容,其中 是在

之后添加的(基于 ) 当条件 seg[@corresp] 满足:

<p><sup>1</sup>在非亨德雷特米图斯中.塞入posuere eros, 坐在 amet pharetra lacus.</p><sup>2</sup><p>Nullam semper varius justo, vitae mollis turpisdapibus 坐在 amet.Donec <sup>3</sup>颞肌</p><p>整数 id ante nunc.Curabitur at ligula sedarcu consequat gravida et id orci.莫尔比之门多洛尔.</p><p>Sed dictum sem<sup>4</sup>nec urna sodales cursus.Donec sat amet nibh tempor, congue ligula semper,</p><sup>5</sup><div>

我可以让它们在单独的模板中工作(在创建 html <p/> 时),但不能在一个模板中工作.但是,在单独的模板中会重新开始编号.

非常感谢.

解决方案

您可以使用不同模式的模板来创建数字并在模式中包含 seg[@corresp] 元素(如我已经在 https://xsltfiddle.liberty-development.net/pPqsHUb) 但作为xsl:number 根据源文档中节点的位置工作,您没有得到您所指示的顺序,因为基本上 seg[@corresp] 元素是这样的以较小的数字编号作为其子元素或后代 datenote 元素.

所以基本上我认为你需要运行一个两步转换,在 seg[@corresp],然后在第二步中给它们编号和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"版本=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/><标记/></xsl:模板><xsl:variable name="markers- added"><xsl:apply-templates mode="add-marker"/></xsl:变量><xsl:template match="/"><xsl:apply-templates select="$markers- added/node()"/></xsl:模板><xsl:template match="p"><div><xsl:apply-templates/></div></xsl:模板><xsl:template match="seg[@type='a']"><p><xsl:apply-templates/></p></xsl:模板><xsl:template match="seg//date[@type='doc_date'] |seg//note[@type='public_note'] |标记"><xsl:apply-templates select="."模式=数字"/></xsl:模板><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:模板></xsl:stylesheet>

https://xsltfiddle.liberty-development.net/pPqsHUb/1

我在那里使用了 XSLT 3 xsl:mode 声明,但它可以被身份转换模板替换,例如

<xsl:copy><xsl:apply-templates select="@* | node()" mode="#current"/></xsl:copy></xsl:模板>

对于 XSLT 2 处理器.

I have tei:xml documents which I am transforming into HTML with XSLT 2.0. The effective structure of the tei documents look like this:

...
<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>
...

In several <xsl:template>s I transform the xml to HTML and thencycle through the tei document to identify elements that need to be transformed into superscript footnote numbers. I use the <xsl:number function> to increment the number:

 <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>

Producing three <sup/> with incremental values 1, 2, 3:

<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>

The problem that I can't seem to solve is how to output the following, where <sup> is added AFTER <p> (based on <tei:seg>) when the condition seg[@corresp] is met:

<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>

I can get them to work in separate templates (while creating the html <p/>), but not in one template. However, being in separate templates restarts the numbering.

Many thanks in advance.

解决方案

You could use a template in a different mode to create number and include the seg[@corresp] elements in the pattern (as I have done in https://xsltfiddle.liberty-development.net/pPqsHUb) but as xsl:number works based on the position of nodes in the source document you don't get the order you have indicated as basically the seg[@corresp] elements that way are numbered with lower numbers as their child or descendant date or note elements.

So basically I think you need to run a two step transformation, add a note or date or other marker element at the end of the seg[@corresp], then number them and the note/date in a second step:

<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

I have used the XSLT 3 xsl:mode declaration in there but it could be replaced by the identity transformation template e.g.

<xsl:template match="@* | node()" mode="add-marker">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()" mode="#current"/>
  </xsl:copy>
</xsl:template>

for an XSLT 2 processor.

这篇关于XSLT 输出到 HTML:基于另一个元素添加一个带有递增编号的 HTML 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆