我有一个XML文本文件,分为<deposition>
个,每个都标有<footnote>
和<appnote>
,其中包含用于脚注目的的识别号码和字母。现在我想循环遍历那些(XSL 3.0 / Saxon),然后获得紧接在它之前的元素的属性来填充新的脚注&#39;块&#39;在每个<footnotes>
的末尾调用<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>
,我希望在每个seg
的最后deposition
之后显示以下内容:
<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
。
(注意:此示例是通用的,前一个元素可能是众多名称之一,因此对元素名称进行硬编码不是一个可行的解决方案。)
很多,非常感谢提前为这个庞大的项目提供所有帮助!
答案 0 :(得分:2)
我想你只想要
<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-of
,你可以使用例如。
<xsl:value-of select="., preceding-sibling::*[1]/@n" separator="="/>
http://xsltfiddle.liberty-development.net/948Fn5a/18或
<xsl:value-of select=". || '=' || preceding-sibling::*[1]/@n"/>