XSL 获取前一个元素/@attribute 的值

XSL getting value of immediately preceding element/@attribute

我有一个 XML 文本文件,分为 <deposition>s,每个都用 <footnote><appnote> 标记,包含用于脚注目的的识别数字和字母。现在我想循环浏览那些 (XSL 3.0/Saxon),然后获取元素 紧挨着它 的属性来填充一个名为 <footnotes> 的新脚注 'block'在每个 <deposition> 的末尾。可以在这里找到测试 http://xsltfiddle.liberty-development.net/948Fn5a/16

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><appnote>a</appnote> ultrices consequat facilisis. 
            Suspendisse a odio<note n="def">foo note</note><footnote>1</footnote> 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><footnote>2</footnote> purus. Praesent 
            aliquam viverra tellus<note n="jkl">another note</note><footnote>3</footnote> in condimentum.</seg><footnote>4</footnote>
    </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><footnote>1</footnote> montes, 
            nascetur ridiculus mus.</seg><footnote>2</footnote>
        <seg n="seg2" type="foo">Curabitur pulvinar leo eget. Orci varius 
            natoque penatibus<note2  n="pqr">another note 2</note2><appnote>a</appnote> et 
            magnis dis<note n="stu">foo note</note><footnote>3</footnote> montes, 
            nascetur ridiculus mus.</seg><footnote>4</footnote>
        <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><footnote>1</footnote> montes, 
            nascetur ridiculus mus.</seg><footnote>2</footnote>
        <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><appnote>a</appnote> pellentesque 
            malesuada elit sed tempor.</seg>
    </text>
</deposition>
</corpus>

XSL:

<?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"/>

<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="deposition">
  <deposition>
    <xsl:apply-templates/> 
    <footnotes>
        <xsl:for-each select="text//footnote">
            <xsl:variable name="pos" select="./current() - 1"/>
            <footitem>
                <xsl:value-of select=". || '='"/><xsl:value-of select="preceding::node()[$pos]/@n"/>
            </footitem>
        </xsl:for-each>
        <xsl:for-each select="text//appnote">
            <xsl:variable name="pos" select="./current() - 1"/>
            <appitem>
                <xsl:value-of select=". || '='"/><xsl:value-of select="preceding::node()[$pos]/@n"/>
            </appitem>
        </xsl:for-each>
    </footnotes>
  </deposition>
</xsl:template>

</xsl:stylesheet>

然而结果出乎意料,可能是由于误解 current()preceding::node

例如,对于第一个 <deposition>,我希望在每个 deposition 中的最后一个 seg 之后出现以下内容:

<footnotes>
  <footitem>1=def</footitem>
  <footitem>2=ghi</footitem>
  <footitem>3=jkl</footitem>
  <footitem>4=seg1</footitem>
</footnotes>
<appnotes>
  <appitem>a=abc</appitem>
</appnotes>

但我得到 Error executing XSLT ....cannot convert string "a" to double

(注意:这个例子是通用的,前面的元素可能是许多名称之一,所以硬编码元素的名称不是一个可行的解决方案。)

非常非常感谢您为这个庞大的项目提供的所有帮助!

我想你只是想要

        <xsl:for-each select="text//footnote">
            <footitem>
                <xsl:value-of select=". || '='"/><xsl:value-of select="preceding-sibling::*[1]/@n"/>
            </footitem>
        </xsl:for-each>
         <xsl:for-each select="text//appnote">
            <appitem>
                <xsl:value-of select=". || '='"/><xsl:value-of select="preceding-sibling::*[1]/@n"/>
            </appitem>
        </xsl:for-each>

尽管这给出了 (http://xsltfiddle.liberty-development.net/948Fn5a/17)

<footnotes><footitem>1=def</footitem><footitem>2=ghi</footitem><footitem>3=jkl</footitem><footitem>4=seg2</footitem><appitem>a=abc</appitem></footnotes></deposition>

所以 4=seg2 而不是 4=seg1

而且你真的不需要两个相邻的 value-ofs,你可以使用例如

<xsl:value-of select="., preceding-sibling::*[1]/@n" separator="="/>

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

                <xsl:value-of select=". || '=' || preceding-sibling::*[1]/@n"/>

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