控制正确更新值的处理顺序

时间:2018-01-07 13:42:08

标签: xml xslt xpath

使用大型XML语料库,我正在集成两组XSL 3.0代码来构建脚注 XSL numbering using two different sets simultaneously(创建数字)和XSL getting value of immediately preceding element/@attribute(创建备注)。

组合代码可在此处找到:http://xsltfiddle.liberty-development.net/948Fn5a/24

下面是上面链接的xsltfiddle上的XML和XSL 3.0,由Saxon处理。我会立即发布问题:

使用XSL <xsl:value-of select=". || '=' || preceding-sibling::*[1]/@n"/>,我们希望appnote / footnote中的数字或字母在块footnotes中返回。正确返回preceding-sibling的内容,显然XPATH的目标是正确的元素。

这是XSL内处理顺序的问题吗?以某种方式创建footnote,然后创建footnote-block,最后footnote收到相应的数字/字母?这需要另一个mode吗?

XML:

<?xml version="1.0" encoding="UTF-8"?>
<corpus>
  <deposition>
    <deposition-title>Praesent vitae</deposition-title>
    <text>
        <seg n="seg1" type="not_foo">Lorem ipsum dolor sit amet, consectetur 
            adipiscing elit. Vivamus<note2 n="abc">another note 2</note2> ultrices consequat facilisis. 
            Suspendisse a odio<note n="def">foo note</note> in lobortis. Aenean 
            non dui scelerisque, rutrum est at, cursus sem.</seg>
        <seg n="seg2" type="foo">Ut pharetra bibendum ipsum, portitor 
            velit pharetra quis. Aeneano<note n="ghi">foo note</note> purus. Praesent 
            aliquam viverra tellus<note n="jkl">another note</note> in condimentum.</seg>
    </text>
  </deposition>
  <deposition>
    <deposition-title>Elementum arcu non</deposition-title>
    <text>
        <seg n="seg1" type="foo">Curabitur pulvinar leo eget. Orci varius 
            natoque penatibus et magnis dis<note n="mno">foo note</note> montes, 
            nascetur ridiculus mus.</seg>
        <seg n="seg2" type="foo">Curabitur pulvinar leo eget. Orci varius 
            natoque penatibus<note2  n="pqr">another note 2</note2> et 
            magnis dis<note n="stu">foo note</note> montes, 
            nascetur ridiculus mus.</seg>
        <seg n="seg3" 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>
  <deposition>
    <deposition-title>Elementum arcu non</deposition-title>
    <text>
        <seg n="seg1" type="foo">Curabitur pulvinar leo eget. Orci varius 
            natoque penatibus et magnis dis<note n="vwx">foo note</note> montes, 
            nascetur ridiculus mus.</seg>
        <seg n="seg2" type="not_foo">Morbi vehicula dolor bibendum enim mollis lobortis. 
            Nulla rutrum vel diam vel posuere. Aliquam<note2  n="yz">another note 2</note2> pellentesque 
            malesuada elit sed tempor.</seg>
    </text>
  </deposition>
  </corpus>

XSL添加了脚注编号,然后在deposition底部创建了一个“块”注释:

<?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"
exclude-result-prefixes="xs"
version="3.0">

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

<!-- ADD FOOTNOTE NUMBERING, IN TWO DIFFERENT SETS: 1, 2, 3... and a, b, c... -->
<xsl:mode name="add-notes" on-no-match="shallow-copy"/>

<xsl:variable name="notes">
  <xsl:apply-templates mode="add-notes"/>
</xsl:variable>

<xsl:template match="seg[@type = 'foo'] | note" mode="add-notes">
  <xsl:next-match/>
  <footnote/>
</xsl:template>

<xsl:template match="note2" mode="add-notes">
  <xsl:next-match/>
  <appnote/>
</xsl:template>

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

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

<xsl:template match="appnote">
  <xsl:copy>
      <xsl:number level="any" format="a" from="deposition"/>
  </xsl:copy>
</xsl:template>

<!-- CREATE FOOTNOTE BLOCK FROM NUMBERING ABOVE -->
<xsl:template match="deposition">
  <deposition>
    <xsl:apply-templates/> 
    <footnote-block>
        <xsl:for-each select="text//footnote">
            <footitem>
                <xsl:value-of select=". || '=' || preceding-sibling::*[1]/@n"/>
            </footitem>
        </xsl:for-each>
         <xsl:for-each select="text//appnote">
            <appitem>
                <xsl:value-of select=". || '=' || preceding-sibling::*[1]/@n"/>
            </appitem>
        </xsl:for-each>
    </footnote-block>
  </deposition>
</xsl:template>

1 个答案:

答案 0 :(得分:1)

是的,基本上,目前您正在尝试插入数字并在同一处理步骤中提取它,并且找不到数字,因此,为了遵循与以前相同的模式,您需要将步骤分开另一种模式:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="3.0">

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

  <!-- ADD FOOTNOTE  -->
  <xsl:mode name="add-notes" on-no-match="shallow-copy"/>

  <!-- number footnotes and notes IN TWO DIFFERENT SETS: 1, 2, 3... and a, b, c... -->
  <xsl:mode name="number-notes" on-no-match="shallow-copy"/>

  <xsl:variable name="notes">
      <xsl:apply-templates mode="add-notes"/>
  </xsl:variable>

  <xsl:template match="seg[@type = 'foo'] | note" mode="add-notes">
      <xsl:next-match/>
      <footnote/>
  </xsl:template>

  <xsl:template match="note2" mode="add-notes">
      <xsl:next-match/>
      <appnote/>
  </xsl:template>

  <xsl:variable name="numbered-notes">
      <xsl:apply-templates select="$notes/node()" mode="number-notes"/>
  </xsl:variable>

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

  <xsl:template match="footnote" mode="number-notes">
      <xsl:copy>
          <xsl:number level="any" format="1" from="deposition"/>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="appnote" mode="number-notes">
      <xsl:copy>
          <xsl:number level="any" format="a" from="deposition"/>
      </xsl:copy>
  </xsl:template>

    <!-- CREATE FOOTNOTE BLOCK FROM NUMBERING ABOVE -->
    <xsl:template match="deposition">
      <deposition>
        <xsl:apply-templates/> 
        <footnote-block>
            <xsl:for-each select="text//footnote">
                <footitem>
                    <xsl:value-of select=". || '=' || preceding-sibling::*[1]/@n"/>
                </footitem>
            </xsl:for-each>
             <xsl:for-each select="text//appnote">
                <appitem>
                    <xsl:value-of select=". || '=' || preceding-sibling::*[1]/@n"/>
                </appitem>
            </xsl:for-each>
        </footnote-block>
      </deposition>
    </xsl:template>


</xsl:stylesheet>

http://xsltfiddle.liberty-development.net/948Fn5a/25