0

我是 XSLT 的新手,并且对 XSLT 中的换行符有类似的要求以获取逗号分隔值,但是我需要执行以下操作

  1. 检查该值是否为单个字符串,例如 Apples,或者是否为逗号分隔列表,例如 Apples、Pears 等
  2. 如果它是逗号分隔列表,则检查列表是否具有特定值或值
  3. 如果值存在,那么做一些事情
  4. 如果它是单个字符串,例如 Apple 然后做一些事情

XSLT 2.0 如何实现这一点?

4

1 回答 1

2

您可以标记列表。像这样的函数可能会完成这项工作(为了演示目的,嵌入到工作 XSLT 中):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="NS:MY">
  <xsl:function name="my:test">
    <xsl:param name="string"/>
    <xsl:variable name="tokens" select="tokenize(normalize-space($string),'\s*,\s*')"/>
    <xsl:choose>
      <xsl:when test="normalize-space($string)='Apples'">
        Do something where we have a single "Apples" 
      </xsl:when>
      <xsl:when test="normalize-space($string)='Pears'"> 
        Do something where we have a single "Pears" 
      </xsl:when>
      <xsl:when test="$tokens='Apples' and $tokens='Pears'"> 
        There are Apples and Pears 
      </xsl:when>
      <xsl:when test="$tokens='Apples'"> 
        There are Apples in the list 
      </xsl:when>
      <xsl:when test="$tokens='Pears'"> 
        There are Pears in the list 
      </xsl:when>
      <xsl:otherwise> 
        Didn't find what we're looking for 
      </xsl:otherwise>
    </xsl:choose>
  </xsl:function>

  <xsl:template match="/">
    <out>
      <xsl:value-of select="my:test('Apples')"/>
      <xsl:value-of select="my:test('Pears,Oranges')"/>
      <xsl:value-of select="my:test(' Apples ,Pears,Oranges')"/>
      <xsl:value-of select="my:test('Oranges , Bananas, Strawberries')"/>
    </out>
  </xsl:template>
</xsl:stylesheet>

您也可以使用正则表达式

See the XPath documentation for what normalize-space() and tokenize() do. You might want to replace the bogus namespace "NS:MY" with something sensible.

于 2012-11-30T00:32:10.907 回答