You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用XSL 3.0的xsl:key获取关联seg对应的deposition-title

XSLT 3.0: Retrieve ancestor <deposition-title> for all <seg> elements matching a @corresp value

Looks like you're almost there with your key approach—you just need to chain the ancestor axis to the result of your key lookup. Let's break this down step by step.

First, Clarify the Key Definition

Your initial key is on the right track, but let's rename it for clarity to explicitly reflect it groups <seg> elements by their @corresp attribute:

<xsl:key name="seg-by-corresp" match="tei:seg" use="@corresp"/>

This key will let you quickly fetch all <seg> elements that share the same @corresp value, which is exactly what you need to find their associated deposition titles.

Retrieve the Ancestor Titles

Once the key is defined, you can directly append the ancestor axis to the key's result to navigate up to the <deposition-title> elements. Here's a complete working example:

<xsl:stylesheet version="3.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:tei="http://www.tei-c.org/ns/1.0">
    
    <xsl:output method="text" encoding="UTF-8"/>
    
    <!-- Key to group all <seg> elements by their @corresp value -->
    <xsl:key name="seg-by-corresp" match="tei:seg" use="@corresp"/>
    
    <xsl:template match="/">
        <!-- Iterate over every <seg> with a @corresp attribute -->
        <xsl:for-each select="//tei:seg[@corresp]">
            <xsl:variable name="current-corresp" select="@corresp"/>
            
            <!-- Output context for readability -->
            <xsl:text>Seg @id="</xsl:text>
            <xsl:value-of select="@id"/>
            <xsl:text>" (corresp="</xsl:text>
            <xsl:value-of select="$current-corresp"/>
            <xsl:text>") links to deposition titles: </xsl:text>
            
            <!-- Get matching <seg> elements, then navigate to their ancestor titles -->
            <xsl:value-of select="key('seg-by-corresp', $current-corresp)/ancestor::tei:deposition/tei:deposition-title" 
                          separator=", "/>
            
            <xsl:text>&#10;</xsl-text> <!-- New line for clean output -->
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Key Details to Note

  1. Chaining the Ancestor Axis: The expression key('seg-by-corresp', $current-corresp)/ancestor::tei:deposition/tei:deposition-title works because key() returns a node set of <seg> elements. We immediately follow this with the ancestor axis to jump up to their parent <deposition> element, then select the child <deposition-title>.
  2. Handling Duplicates: If multiple <seg> elements from the same deposition share the same @corresp value, you'll get duplicate titles. To fix this, use distinct-values() (for plain text titles) or distinct-nodes() (if titles contain markup):
    <xsl:value-of select="distinct-values(key('seg-by-corresp', $current-corresp)/ancestor::tei:deposition/tei:deposition-title)" 
                  separator=", "/>
    
  3. Namespace Awareness: Ensure your XSLT declares the tei namespace (matching your XML's namespace) so all selectors work correctly.

Example Output for Your Sample XML

Running this on your provided corpus would output:

Seg @id="2-A" (corresp="1-B") links to deposition titles: foo title A
Seg @id="2-C" (corresp="1-A") links to deposition titles: foo title B, foo title C
Seg @id="3-B" (corresp="1-A") links to deposition titles: foo title B, foo title C
Seg @id="4-A" (corresp="2-B") links to deposition titles: foo title B
Seg @id="4-B" (corresp="2-A") links to deposition titles: foo title A

内容的提问来源于stack exchange,提问作者jbrehr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:18:14