使用XSL 3.0的xsl:key获取关联seg对应的deposition-title
<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> </xsl-text> <!-- New line for clean output --> </xsl:for-each> </xsl:template> </xsl:stylesheet>
Key Details to Note
- Chaining the Ancestor Axis: The expression
key('seg-by-corresp', $current-corresp)/ancestor::tei:deposition/tei:deposition-titleworks becausekey()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>. - Handling Duplicates: If multiple
<seg>elements from the same deposition share the same@correspvalue, you'll get duplicate titles. To fix this, usedistinct-values()(for plain text titles) ordistinct-nodes()(if titles contain markup):<xsl:value-of select="distinct-values(key('seg-by-corresp', $current-corresp)/ancestor::tei:deposition/tei:deposition-title)" separator=", "/> - Namespace Awareness: Ensure your XSLT declares the
teinamespace (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

