Xslt 获取具有不同语言的不同规则的字符串之间的文本

Xslt get text between strings with different rules for different languages

我正在使用 xslt 解析 Html 页面。 在页面上有一个 html 的和平,我想从中接收出版商名称

<div>
    <span class="publisher_name">by xxx from TripAdvisor</span>
</div>

为了解析它,我使用下一个代码:

<xsl:variable name="publisherTextNode" select=".//span[@class='publisher_name'][1]"/>
    <xsl:if test="$publisherTextNode">
        <Publisher>
            <xsl:call-template name="string-trim">
                <xsl:with-param name="string" select="substring-before(substring-after($publisherTextNode, 'by'), 'from')" />
            </xsl:call-template>
         </Publisher>
    </xsl:if>

所以它应该 select 在 byfrom 之间的文本。结果应该是 xxx

但是对于非 English.

的语言来说,这是一个问题

在西班牙语的情况下 html 看起来像

<span class="publisher_name">por xxx de TripAdvisor</span>

xslt returns string.Empty 因为它找不到 by 字符串。

所以我想添加类似的规则来支持西班牙语字符串也像

<xsl:with-param name="string" select="substring-before(substring-after($publisherTextNode, 'por'), 'de')" />

我能否以某种方式将这 2 条规则添加到现有的 xslt 模式(也许检查第一条规则 returns string.Empty 然后使用第二条规则?)或为不同的语言创建单独的规则?


  <xsl:template name="string-trim">
<xsl:param name="string" />
<xsl:param name="trim" select="$whitespace" />
<xsl:call-template name="string-rtrim">
  <xsl:with-param name="string">
    <xsl:call-template name="string-ltrim">
      <xsl:with-param name="string" select="$string" />
      <xsl:with-param name="trim"   select="$trim" />
    </xsl:call-template>
  </xsl:with-param>
  <xsl:with-param name="trim"   select="$trim" />
</xsl:call-template>

<xsl:template name="string-ltrim">
<xsl:param name="string" />
<xsl:param name="trim" select="$whitespace" />

<xsl:if test="string-length($string) &gt; 0">
  <xsl:choose>
    <xsl:when test="contains($trim, substring($string, 1, 1))">
      <xsl:call-template name="string-ltrim">
        <xsl:with-param name="string" select="substring($string, 2)" />
        <xsl:with-param name="trim"   select="$trim" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$string" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:if>

  <xsl:template name="string-rtrim">
<xsl:param name="string" />
<xsl:param name="trim" select="$whitespace" />

<xsl:variable name="length" select="string-length($string)" />

<xsl:if test="$length &gt; 0">
  <xsl:choose>
    <xsl:when test="contains($trim, substring($string, $length, 1))">
      <xsl:call-template name="string-rtrim">
        <xsl:with-param name="string" select="substring($string, 1, $length - 1)" />
        <xsl:with-param name="trim"   select="$trim" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$string" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:if>

怎么样(XSLT 1.0):

<xsl:call-template name="string-trim">
    <xsl:with-param name="string">
        <xsl:choose>
            <xsl:when test="contains($publisherTextNode, 'by ') and contains($publisherTextNode, ' from')">
                <xsl:value-of select="substring-before(substring-after($publisherTextNode, 'by '), ' from')" />
            </xsl:when>
            <xsl:when test="contains($publisherTextNode, 'por ') and contains($publisherTextNode, ' de')">
                <xsl:value-of select="substring-before(substring-after($publisherTextNode, 'por '), ' de')" />
            </xsl:when>
        </xsl:choose>
    </xsl:with-param>
</xsl:call-template>

请注意,测试中出现误报的可能性很小。