如何用XSLT 1.0提取LotId节点动态内容间的指定子串?
Got it, let's figure out how to extract that middle segment from your <LotId> value. The pattern here is consistent: dynamic_text_target_text_dynamic_text separated by underscores, and you need the middle part. Here are reliable solutions based on which XSLT version you're using:
If you're working with XSLT 2.0 or later (like Saxon 9+), the built-in tokenize() function makes this trivial. It splits your string into a sequence using underscores as the delimiter, then you just pick the 2nd element in the sequence (XSLT uses 1-based indexing):
<xsl:template match="LotId"> <!-- Split the string by underscores, select the second segment --> <xsl:value-of select="tokenize(., '_')[2]"/> </xsl:template>
For your example input NOIDEHOB_NOIDE1_4321-123, this will output exactly NOIDE1 regardless of what the first and third segments are, as long as the underscore separator stays consistent.
XSLT 1.0 doesn't have tokenize(), so we'll need a recursive template to handle the string splitting. This approach is fully compatible with all XSLT 1.0 processors and doesn't rely on any extensions:
First, define a reusable template to get the Nth token from a split string:
<!-- Reusable template to fetch the Nth token from a string split by a delimiter --> <xsl:template name="get-nth-token"> <xsl:param name="input-string"/> <xsl:param name="delimiter" select="'_'"/> <xsl:param name="n" select="1"/> <xsl:choose> <!-- When we've reached the target token, output it --> <xsl:when test="$n = 1"> <xsl:value-of select="substring-before(concat($input-string, $delimiter), $delimiter)"/> </xsl:when> <!-- If there are more tokens left, recurse with the remaining string --> <xsl:when test="contains($input-string, $delimiter)"> <xsl:call-template name="get-nth-token"> <xsl:with-param name="input-string" select="substring-after($input-string, $delimiter)"/> <xsl:with-param name="delimiter" select="$delimiter"/> <xsl:with-param name="n" select="$n - 1"/> </xsl:call-template> </xsl:when> <!-- Fallback: if there are fewer tokens than requested, output empty --> <xsl:otherwise/> </xsl:choose> </xsl:template>
Then use this template in your match for <LotId>:
<xsl:template match="LotId"> <xsl:call-template name="get-nth-token"> <xsl:with-param name="input-string" select="."/> <xsl:with-param name="n" select="2"/> <!-- We want the 2nd token --> </xsl:call-template> </xsl:template>
Optional XSLT 1.0 Approach (With EXSLT Extensions)
If your processor supports EXSLT (like Xalan or Saxon), you can use regex replacement to grab the middle segment in one line:
<xsl:value-of select="regexp:replace(., '^.*?_([^_]+)_.*$', '$1')"/>
This regex matches everything up to the first underscore, captures the segment between the first and second underscore, then matches everything after the second underscore. The replacement uses the captured group ($1) to output just the middle part. Note that this depends on your processor enabling EXSLT support, so the recursive template is more universally reliable.
内容的提问来源于stack exchange,提问作者stianzz




