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

如何使用XSLT获取被引用when元素的interval属性值?

Solution to Retrieve Referenced <when> Element's interval Attribute

Got it, let's tackle this problem—you need to pull the interval attribute from a <when> element that's referenced by the start attribute of an <annotationBlock>, right? Your current template's select statement isn't working because it can't cross-reference the xml:id link between the two elements. Here are two solid solutions depending on your XSLT version:

First, let's assume your XML structure looks something like this (to ground the examples):

<root>
  <when xml:id="time-point-1" interval="PT15M"/>
  <when xml:id="time-point-2" interval="PT30S"/>
  
  <annotationBlock start="#time-point-1">
    <u>
      <w>Sample annotated text</w>
    </u>
  </annotationBlock>
</root>

Method 1: Use key() (Compatible with XSLT 1.0+)

This is the most reliable approach for older XSLT versions. First, define a key at the top level of your stylesheet to map <when> elements to their xml:id values:

<xsl:key name="when-by-id" match="when" use="@xml:id"/>

Then, in your template matching annotationBlock/u/w, extract the target ID from the start attribute (strip the # prefix), use the key to find the matching <when> element, and grab its interval attribute:

<xsl:template match="annotationBlock/u/w">
  <!-- Extract the ID from the start attribute (remove the #) -->
  <xsl:variable name="target-when-id" select="substring(../../@start, 2)"/>
  
  <!-- Use the key to locate the corresponding <when> element -->
  <xsl:variable name="matching-when" select="key('when-by-id', $target-when-id)"/>
  
  <!-- Output the interval attribute value -->
  <xsl:value-of select="$matching-when/@interval"/>
</xsl:template>

If you're using XSLT 2.0 or later, the id() function simplifies this process—it can directly handle either the raw xml:id value or the reference string with the # prefix:

<xsl:template match="annotationBlock/u/w">
  <!-- Option 1: Strip the # first -->
  <xsl:value-of select="id(substring(../../@start, 2))/@interval"/>
  
  <!-- Option 2: XSLT 2.0+ allows id() to accept the #-prefixed string directly -->
  <!-- <xsl:value-of select="id(../../@start)/@interval"/> -->
</xsl:template>

Why Your Original select Failed

Your initial select statement was likely only accessing attributes of the current node or its immediate parent/children. To cross-reference elements by their xml:id, you need to use either key() or id() to look up the target element across the entire document tree.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 07:09:50