简体   繁体   English

从XSLT 1.0中的字符串中提取多个子字符串

[英]Extracting multiple substrings from a string in XSLT 1.0

Spent far too long on this seemingly impossible issue today, I'm at my wits end. 今天在这个看似不可能的问题上花费了太长时间,我不知所措。 Would appreciate any help, have search stackoverflow high and low. 将不胜感激,有搜索堆栈高低。

I have a string I'm trying to manipulate with XSLT eg 我有一个用XSLT操纵的字符串,例如

' man START red END woman START child END rabbit START goose END blue ' ' 男人START红色END女人START儿童END兔子START鹅END蓝色 '

I'm trying to extract all substrings between START and END, and concat into one new string. 我正在尝试提取START和END之间的所有子字符串,并将concat合并为一个新字符串。

So the resulting string should be: 因此,结果字符串应为:

' red child goose ' 红鹅儿

Thanks - Rob 谢谢-罗伯

We do not know where this string comes from, but assuming an input document like 我们不知道此字符串来自何处,但假设输入文档为

XML Input XML输入

<?xml version="1.0" encoding="UTF-8"?>
<input>man START red END woman START child END rabbit START goose END blue</input>

XSLT Stylesheet XSLT样式表

Write a recursive named template that looks for occurrences of START and END in the string. 编写一个递归的命名模板,以查找字符串中STARTEND出现。

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" />

    <xsl:template match="input">
      <output>
          <xsl:call-template name="find-relevant-text">
              <xsl:with-param name="string" select="."/>
          </xsl:call-template>
      </output>
    </xsl:template>

    <xsl:template name="find-relevant-text">
        <xsl:param name="string"/>

        <xsl:if test="contains($string,'START')">
            <xsl:value-of select="substring-before(substring-after($string,'START '),'END')"/>
            <xsl:call-template name="find-relevant-text">
                <xsl:with-param name="string" select="substring-after($string,'END ')"/>
            </xsl:call-template>
        </xsl:if>

    </xsl:template>

</xsl:transform>

XML Output XML输出

<?xml version="1.0" encoding="UTF-8"?>
<output>red child goose </output>

If you are concerned about the whitespace character at the end, use 如果您担心结尾的空白字符,请使用

<xsl:template name="find-relevant-text">
    <xsl:param name="string"/>

    <xsl:if test="contains($string,'START')">

        <xsl:variable name="relevant-part" select="substring-before(substring-after($string,'START '),' END')"/>
        <xsl:variable name="remainder" select="substring-after($string,'END ')"/>

        <xsl:value-of select="$relevant-part"/>
        <xsl:if test="contains($remainder,'START')">
            <xsl:text> </xsl:text>
        </xsl:if>
        <xsl:call-template name="find-relevant-text">
            <xsl:with-param name="string" select="$remainder"/>
        </xsl:call-template>
    </xsl:if>

</xsl:template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM