【问题标题】:Iterating through an XSLT string parameter and doing something for each parameter遍历 XSLT 字符串参数并为每个参数做一些事情
【发布时间】:2011-12-24 22:15:16
【问题描述】:

我正在尝试基于 XSLT 字符串参数构建一些 XML 元素。这个想法很简单。

用户指定一个参数,它是一个以逗号分隔的名称列表。对于每个名称,我想将以下内容写入 XML 文件:

<category>{$string}</category>

其中{$string} 是逗号分隔值之一。所以如果用户提供了这个参数:category1, category2, cat3,那么我应该在 XML 文件中得到这个:

<category>category1</category>
<category>category2</category>
<category>cat3</category>

也应该可以提供一个空字符串,在这种情况下不会打印任何 XML 元素。

请注意,我使用的是 XSLT 2.0,因此请随意包含基于 2.0 的 XPath 和 XSLT 构造。

【问题讨论】:

    标签: xslt xslt-2.0


    【解决方案1】:

    这种转换 (XSLT 2.0):

    <xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:param name="pCategories"
     select="'category1, category2, cat3'"/>
    
     <xsl:template match="/">
      <xsl:for-each select="tokenize($pCategories, ',')[.]">
       <category>
        <xsl:value-of select="normalize-space()"/>
        </category>
      </xsl:for-each>
     </xsl:template>
    </xsl:stylesheet>
    

    当应用于任何 XML 文档(未使用)时,会产生所需的正确结果

    <category>category1</category>
    <category>category2</category>
    <category>cat3</category>
    

    【讨论】:

      猜你喜欢
      • 2014-12-29
      • 2014-06-26
      • 1970-01-01
      • 2015-07-17
      • 2012-05-08
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多