jbrehr
jbrehr

Reputation: 815

XSL getting ancestors from xsl:key

I have an XML corpus that is structured like this (in reality it contains over 5000 <deposition>elements and about 20,000 combined <seg> elements):

<corpus>
  <deposition id='1'>
    <deposition-title>foo title A<deposition-title>
    <text>
      <seg id='1-A'>some text</seg>
      <seg id='1-B'>some text</seg>
    </text>
  </deposition>
  <deposition id='2'>
    <deposition-title>foo title B<deposition-title>
    <text>
      <seg id='2-A' corresp='1-B'>some text</seg>
      <seg id='2-B'>some text</seg>
      <seg id='2-C' corresp='1-A'>some text</seg>
    </text>
  </deposition>
  <deposition id='3'>
    <deposition-title>foo title C<deposition-title>
    <text>
      <seg id='3-A'>some text</seg>
      <seg id='3-B' corresp='1-A'>some text</seg>
    </text>
  </deposition>
  <deposition id='4'>
    <deposition-title>foo title D<deposition-title>
    <text>
      <seg id='4-A' corresp='2-B'>some text</seg>
      <seg id='4-B' corresp='2-A'>some text</seg>
      <seg id='4-C'>some text</seg>
    </text>
  </deposition>
  [...]
</corpus>

The seg/@corresp refers to the seg/@id> of other entries. The logic, therefore, is that one can have many seg/@corresp's that match a single seg/ @id.

My objective is this in XSL 3.0 : for each seg[@corresp] find all other ancestor::deposition-titles for seg elements that contain the same attribute value.

An example result would be:

Passing @corresp="1-A" to a key would return a list of seg-ids which share the same @corresp: foo title B, foo title c

I am now trying to execute this using a key in a xsl:for-each:

  1. use a key to efficiently get all seg/@id : <xsl:key name="segid" match="tei:seg" use="@corresp"/>

  2. apply current seg/@corresp to the key (generally <xsl:value-of select="key('segid', seg/@corresp)">)

and here is the part I can't figure out in XSL, using a select inside a 'for-each' to get the ancestor of the seg returned by the key:

  1. for each seg/@corresp output the deposition-title siblings of nodes seg/@corresp matching it

I hope the above is clear.

Many thanks for any assistance.

Upvotes: 0

Views: 172

Answers (1)

Tim C
Tim C

Reputation: 70648

I think what you want is this...

<xsl:value-of select="key('segid', seg/@corresp)/ancestor::deposition/deposition-title">

Or, maybe this, is everything is always at the same level

<xsl:value-of select="key('segid', seg/@corresp)/../../deposition-title">

Or maybe this...

<xsl:value-of select="key('segid', seg/@corresp)/../preceding-sibling::deposition-title">

Where .. gets the parent of a node.

I can't help thinking you actually want to do <xsl:value-of select="key('segid', @id)"> here though (assuming you are currently positioned on a seg element.

Upvotes: 2

Related Questions