XSLT output to HTML: add an HTML element with incremented number, based on another element

霸气de小男生 提交于 2019-12-02 09:15:35

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!