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

如何为文本中图注生成超链接?求匹配figure id的idref实现代码

Solution for Linking Figure Captions to Their Corresponding <figure> IDs

Got it, let's get this sorted for you! The problem with your initial analyze-string attempt was likely either not properly mapping the figure labels to their IDs or not handling case variations in the text references. Here's a working XSLT 2.0+ solution that does exactly what you need:

Working XSLT Code

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- Identity template: copies all content by default -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- Key to quickly look up figure IDs by their label text -->
    <xsl:key name="figure-by-label" match="figure" use="normalize-space(label)"/>

    <!-- Template to process text nodes and replace figure references with links -->
    <xsl:template match="text()">
        <xsl:analyze-string select="." regex="(Figure|figure) (\d+)" flags="i">
            <xsl:matching-substring>
                <!-- Create a label string that matches the exact format in <figure>/<label> -->
                <xsl:variable name="target-label" select="concat('Figure ', $2)"/>
                <!-- Fetch the corresponding figure ID using the key -->
                <xsl:variable name="figure-id" select="key('figure-by-label', $target-label)/@id"/>
                
                <!-- Generate the link if a matching figure exists -->
                <xsl:choose>
                    <xsl:when test="$figure-id">
                        <a idref="{$figure-id}">
                            <xsl:value-of select="."/>
                        </a>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="."/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:matching-substring>
            <xsl:non-matching-substring>
                <xsl:value-of select="."/>
            </xsl:non-matching-substring>
        </xsl:analyze-string>
    </xsl:template>
</xsl:stylesheet>

How This Works

  1. Identity Template: Preserves all existing XML structure unless explicitly modified.
  2. Lookup Key: The <xsl:key> creates a fast mapping between each <figure>'s <label> text and its id attribute, so we don't have to scan the entire document every time we need to find a match.
  3. Text Processing: The analyze-string function scans text nodes for references like Figure 01 or figure 02 (case-insensitive thanks to flags="i"). For each match:
    • We create a standardized label string (matching the format in your <figure>'s <label>) to look up the correct ID.
    • If a matching <figure> exists, we replace the text with an <a> element whose idref matches the figure's id. If no match is found, we leave the original text as-is.

Example Input & Output

Input XML

<book>
    <figure id="ch01fig01">
        <label>Figure 01</label>
        <figcaption>xxx</figcaption>
    </figure>
    <figure id="ch01fig02">
        <label>Figure 02</label>
        <figcaption>yyy</figcaption>
    </figure>
    <p>This is a Figure 01 and this is figure 02</p>
</book>

Output XML

<book>
    <figure id="ch01fig01">
        <label>Figure 01</label>
        <figcaption>xxx</figcaption>
    </figure>
    <figure id="ch01fig02">
        <label>Figure 02</label>
        <figcaption>yyy</figcaption>
    </figure>
    <p>This is a <a idref="ch01fig01">Figure 01</a> and this is <a idref="ch01fig02">figure 02</a></p>
</book>

Customization Tips

  • If your figure labels use a different format (e.g., Fig. 01 or Chapter 1 Figure 3), adjust the regex pattern and $target-label concatenation to match your actual label text.
  • If you prefer using <xref> instead of <a> for links, just replace the <a> element in the template with <xref>.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:34:17