不同级别的XSL编号

时间:2018-01-05 18:37:33

标签: xml xslt

我有一个XML文本文档,如下所示:

<corpus>
 <deposition>
  <deposition-title>Praesent vitae</deposition-title>
  <text>
   <seg type="not_foo">Lorem ipsum dolor sit amet, consectetur 
     adipiscing elit. Vivamus ultrices consequat facilisis. 
     Suspendisse a odio<note>foo note</note> in lobortis. Aenean 
     non dui scelerisque, rutrum est at, cursus sem.</seg>
   <seg type="foo">Ut pharetra bibendum ipsum, portitor 
     velit pharetra quis. Aeneano<note>foo note</note> purus. Praesent 
     aliquam viverra tellus in condimentum.</seg>
   </text>
 </deposition>
 <deposition>
  <deposition-title>Elementum arcu non</deposition-title>
  <text>
    <seg type="foo">Curabitur pulvinar leo eget. Orci varius 
     natoque penatibus et magnis dis<note>foo note</note> montes, 
     nascetur ridiculus mus.</seg>
    <seg type="not_foo">Morbi vehicula dolor bibendum enim mollis lobortis. 
     Nulla rutrum vel diam vel posuere. Aliquam pellentesque 
     malesuada elit sed tempor.</seg>
   </text>
 </deposition>
</corpus>

我正在添加脚注编号以准备打印,在Saxon上使用XSL 3.0进行一些预处理。目标输出在两个条件<footnote/>上添加seg[@type='foo'] | note加上增量编号。每个deposition的数字重置为零。结果应如下所示:

<corpus>
 <deposition>
  <deposition-title>Praesent vitae</deposition-title>
  <text>
   <seg type="not_foo">Lorem ipsum dolor sit amet, consectetur 
     adipiscing elit. Vivamus ultrices consequat facilisis. 
     Suspendisse a odio<note>some note</note><footnote>1</footnote> in lobortis. Aenean 
     non dui scelerisque, rutrum est at, cursus sem.</seg>
   <seg type="foo">Ut pharetra bibendum ipsum, portitor 
     velit pharetra quis. Aeneano<note>some note</note><footnote>2</footnote> purus. Praesent 
     aliquam viverra tellus in condimentum.</seg><footnote>3</footnote>
   </text>
 </deposition>
 <deposition>
  <deposition-title>Elementum arcu non</deposition-title>
  <text>
    <seg type="foo">Curabitur pulvinar leo eget. Orci varius 
     natoque penatibus et magnis dis<note>some note</note><footnote>1</footnote> montes, 
     nascetur ridiculus mus.</seg><footnote>2</footnote>
    <seg type="not_foo">Morbi vehicula dolor bibendum enim mollis lobortis. 
     Nulla rutrum vel diam vel posuere. Aliquam pellentesque 
     malesuada elit sed tempor.</seg></seg><footnote>3</footnote>
   </text>
 </deposition>
</corpus>

我正在使用以下内容复制并插入带有数字的<footnote>

<xsl:template match="seg[@type='foo'] | note">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy><footnote><xsl:number count="seg[@type='foo'] | note" from="deposition" format="1" level="any"/></footnote>
</xsl:template>

然而,结果并非我的预期。就分配数字而言,这个模板似乎首先处理seg元素,然后处理seg秒内的任何内容?喜欢图层?在第一个deposition的情况下,它会产生这个新的XML:

<deposition>
  <deposition-title>Praesent vitae</deposition-title>
  <text>
   <seg>Lorem ipsum dolor sit amet, consectetur 
     adipiscing elit. Vivamus ultrices consequat facilisis. 
     Suspendisse a odio<note>some note</note><footnote>2</footnote> in lobortis. Aenean 
     non dui scelerisque, rutrum est at, cursus sem.</seg>
   <seg type="foo">Ut pharetra bibendum ipsum, portitor 
     velit pharetra quis. Aeneano<note>some note</note><footnote>3</footnote> purus. Praesent 
     aliquam viverra tellus in condimentum.</seg><footnote>1</footnote>
   </text>
 </deposition>

这意味着脚注不是真实的&#39; (或&#39;可打印&#39;)订单。

不确定是什么原因造成的。感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

我采用了不同的方法,它使用第一个转换步骤添加空的footnote元素,使用第二个转换步骤使用xsl:number对这些元素进行编号:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    xmlns:array="http://www.w3.org/2005/xpath-functions/array"
    exclude-result-prefixes="xs math map array"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:mode name="add-footnotes" on-no-match="shallow-copy"/>

  <xsl:variable name="footnotes">
      <!-- populating variable by pushing the global context item's child node through the mode named 'add-footnotes' -->
      <xsl:apply-templates mode="add-footnotes"/>
  </xsl:variable>

  <xsl:template match="seg[@type = 'foo'] | note" mode="add-footnotes">
      <!-- delegating copying of the element to the identity transformation -->
      <xsl:next-match/>
      <footnote/>
  </xsl:template>

  <xsl:template match="/">
      <xsl:apply-templates select="$footnotes/node()"/>
  </xsl:template>

  <xsl:template match="footnote">
      <xsl:copy>
          <xsl:number level="any" from="deposition"/>
      </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

http://xsltfiddle.liberty-development.net/eiQZDbe/1

的示例

答案 1 :(得分:0)

我认为您无法使用npm ERR! Windows_NT 10.0.16299 npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "bcrypt" npm ERR! node v6.11.0 npm ERR! npm v3.10.10 npm ERR! file C:\Users\Admin\AppData\Roaming\npm-cache\code-point-at\1.1.0\package\package.json npm ERR! code EJSONPARSE npm ERR! Failed to parse json npm ERR! Unexpected token 's' at 1:1 npm ERR! sEDU npm ERR! ^ npm ERR! File: C:\Users\Admin\AppData\Roaming\npm-cache\code-point-at\1.1.0\package\package.json npm ERR! Failed to parse package.json data. npm ERR! package.json must be actual JSON, not just JavaScript. npm ERR! npm ERR! This is not a bug in npm. npm ERR! Tell the package author to fix their package.json file. JSON.parse npm ERR! Please include the following file with any support request: npm ERR! D:\greenstand\2\0105\treetracker-mobile-api-master\treetracker-mobile-api-master\npm-debug.log 执行此操作。

我能想到的唯一方法就是计算前面的xsl:number / seg,添加1(对于我自己),并添加任何后代note

前面的效率不是很高,但希望我忽略了一些东西,你会得到更好的答案。

示例...

XML输入(为测试添加了额外的note

note

XSLT 2.0 (这也是有效的1.0)

<corpus>
    <deposition>
        <deposition-title>Praesent vitae</deposition-title>
        <text>
            <seg type="not_foo">Lorem ipsum dolor sit amet, consectetur 
                adipiscing elit. Vivamus ultrices consequat facilisis. 
                Suspendisse a odio<note>foo note</note> in lobortis. Aenean 
                non dui scelerisque, rutrum est at, cursus sem.</seg>
            <seg type="foo">Ut pharetra bibendum ipsum, portitor 
                velit pharetra quis. Aeneano<note>foo note</note> purus. Praesent 
                aliquam viverra tellus<note>another note</note> in condimentum.</seg>
        </text>
    </deposition>
    <deposition>
        <deposition-title>Elementum arcu non</deposition-title>
        <text>
            <seg type="foo">Curabitur pulvinar leo eget. Orci varius 
                natoque penatibus et magnis dis<note>foo note</note> montes, 
                nascetur ridiculus mus.</seg>
            <seg type="not_foo">Morbi vehicula dolor bibendum enim mollis lobortis. 
                Nulla rutrum vel diam vel posuere. Aliquam pellentesque 
                malesuada elit sed tempor.</seg>
        </text>
    </deposition>
</corpus>

<强>输出

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="seg[@type='foo']|note">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    <xsl:variable name="dep" select="generate-id(ancestor::deposition)"/>
    <footnote>
      <xsl:value-of 
        select="count((preceding::seg[@type='foo']|preceding::note)[generate-id(ancestor::deposition)=$dep]) + 1 + count(.//note)"/>
    </footnote>
  </xsl:template>

</xsl:stylesheet>