如何为文本中图注生成超链接?求匹配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
- Identity Template: Preserves all existing XML structure unless explicitly modified.
- Lookup Key: The
<xsl:key>creates a fast mapping between each<figure>'s<label>text and itsidattribute, so we don't have to scan the entire document every time we need to find a match. - Text Processing: The
analyze-stringfunction scans text nodes for references likeFigure 01orfigure 02(case-insensitive thanks toflags="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 whoseidrefmatches the figure'sid. If no match is found, we leave the original text as-is.
- We create a standardized label string (matching the format in your
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. 01orChapter 1 Figure 3), adjust theregexpattern and$target-labelconcatenation 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
相关产品推荐
相关产品推荐

