Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 一次访问XSLT中的所有拆分字符串_Html_Xml_Xslt - Fatal编程技术网

Html 一次访问XSLT中的所有拆分字符串

Html 一次访问XSLT中的所有拆分字符串,html,xml,xslt,Html,Xml,Xslt,我将按如下方式拆分逗号分隔的字符串: <xsl:call-template name="SimpleStringLoop"> <xsl:with-param name="input" select="'1,2,3,4,'"/> </xsl:call-template> <xsl:template name="SimpleStringLoop"> <xsl:param name="input"/> <xsl

我将按如下方式拆分逗号分隔的字符串:

<xsl:call-template name="SimpleStringLoop">
    <xsl:with-param name="input" select="'1,2,3,4,'"/>
</xsl:call-template>

<xsl:template name="SimpleStringLoop">
    <xsl:param name="input"/>
    <xsl:if test="string-length($input) &gt; 0">
      <xsl:variable name="v" select="substring-before($input, ',')"/>
      <xsl:value-of select="$v"/>
      <xsl:call-template name="SimpleStringLoop">
        <xsl:with-param name="input" select="substring-after($input, ',')"/>
      </xsl:call-template>
    </xsl:if> 
</xsl:template>

如何访问数组中的所有提取值? 我需要访问它们中的每一个,并在xslt过滤中使用它们。 调用SimpleStringLoop时,$v第一次保存值1,另一次保存值2,依此类推。但是有没有一种方法可以像在$v[1]、$v[2]这样的数组中使用索引一样使用$v呢

所以我有,
$v[1]=1,$v[2]=2等等


还是我太笨了以至于我根本不理解这个递归过程??

您需要XSLT2.0。在XSLT1.0中,没有数据类型来处理字符串序列。您可以创建一个包含变量中字符串的节点集,如果您有exslt:node-set()扩展名,则可以处理该节点集,但它是一个糟糕的替代品。升级时间到了。

您需要XSLT2.0。在XSLT1.0中,没有数据类型来处理字符串序列。您可以创建一个包含变量中字符串的节点集,如果您有exslt:node-set()扩展名,则可以处理该节点集,但它是一个糟糕的替代品。是时候升级了。

正如Michael Kay所说,XSLT 2.0将是您的朋友,因为它可以轻松处理序列。事实上,您甚至不需要递归模板,因为您可以使用方便的tokenize函数分割字符串

<xsl:variable name="fields" select="tokenize($input, ',')" />

然后,例如,要得到第二个字段,您可以这样做

<xsl:value-of select="$fields[2]"/>

但是,如果您坚持使用XSLT1.0,您将需要做一些额外的工作。目前,递归模板所做的只是输出一系列字符。它们构成结果树的一部分,因此XSLT最初无法访问它们。不过,此时您需要做的是输出元素

  <xsl:variable name="v" select="substring-before($input, ',')"/>
  <field>
    <xsl:value-of select="$v"/>
  </field>

接下来,您需要将现有的xsl:call模板包装在一个变量中,这样您就可以开始访问模板调用的结果

<xsl:variable name="fields">
  <xsl:call-template name="SimpleStringLoop">
    <xsl:with-param name="input" select="'1,2,3,4,'"/>
  </xsl:call-template>
</xsl:variable>

现在,fields变量此时保存一个“结果树片段”,但您希望能够访问其中的节点。这就是扩展函数的作用。有一个扩展可以将结果树片段转换为节点集,因此可以对其使用XSLT函数。这在很大程度上取决于处理器使用的名称空间,但最终它将允许您编写如下内容:

 <xsl:value-of select="msxml:node-set($fields)/field[2]"/>

以这个XSLT为例。注意,我在这里使用的是Mircosoft的XSLT处理器。其他XSLT处理器可用

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxml="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxml">
  <xsl:template match="/*">
    <xsl:variable name="fields">
      <xsl:call-template name="SimpleStringLoop">
        <xsl:with-param name="input" select="'1,2,3,4,'"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="msxml:node-set($fields)/field[2]"/>
  </xsl:template>

  <xsl:template name="SimpleStringLoop">
    <xsl:param name="input"/>
    <xsl:if test="string-length($input) &gt; 0">
      <xsl:variable name="v" select="substring-before($input, ',')"/>
      <field>
        <xsl:value-of select="$v"/>
      </field>
      <xsl:call-template name="SimpleStringLoop">
        <xsl:with-param name="input" select="substring-after($input, ',')"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

有关节点集的详细信息,请尝试从以下链接开始:


正如Michael Kay所说,XSLT 2.0将是您的朋友,因为它可以轻松处理序列。事实上,您甚至不需要递归模板,因为您可以使用方便的tokenize函数分割字符串

<xsl:variable name="fields" select="tokenize($input, ',')" />

然后,例如,要得到第二个字段,您可以这样做

<xsl:value-of select="$fields[2]"/>

但是,如果您坚持使用XSLT1.0,您将需要做一些额外的工作。目前,递归模板所做的只是输出一系列字符。它们构成结果树的一部分,因此XSLT最初无法访问它们。不过,此时您需要做的是输出元素

  <xsl:variable name="v" select="substring-before($input, ',')"/>
  <field>
    <xsl:value-of select="$v"/>
  </field>

接下来,您需要将现有的xsl:call模板包装在一个变量中,这样您就可以开始访问模板调用的结果

<xsl:variable name="fields">
  <xsl:call-template name="SimpleStringLoop">
    <xsl:with-param name="input" select="'1,2,3,4,'"/>
  </xsl:call-template>
</xsl:variable>

现在,fields变量此时保存一个“结果树片段”,但您希望能够访问其中的节点。这就是扩展函数的作用。有一个扩展可以将结果树片段转换为节点集,因此可以对其使用XSLT函数。这在很大程度上取决于处理器使用的名称空间,但最终它将允许您编写如下内容:

 <xsl:value-of select="msxml:node-set($fields)/field[2]"/>

以这个XSLT为例。注意,我在这里使用的是Mircosoft的XSLT处理器。其他XSLT处理器可用

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxml="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxml">
  <xsl:template match="/*">
    <xsl:variable name="fields">
      <xsl:call-template name="SimpleStringLoop">
        <xsl:with-param name="input" select="'1,2,3,4,'"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="msxml:node-set($fields)/field[2]"/>
  </xsl:template>

  <xsl:template name="SimpleStringLoop">
    <xsl:param name="input"/>
    <xsl:if test="string-length($input) &gt; 0">
      <xsl:variable name="v" select="substring-before($input, ',')"/>
      <field>
        <xsl:value-of select="$v"/>
      </field>
      <xsl:call-template name="SimpleStringLoop">
        <xsl:with-param name="input" select="substring-after($input, ',')"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

有关节点集的详细信息,请尝试从以下链接开始:


您使用的是XSLT 1.0还是2.0?不幸的是XSLT 1.0..XSLT变量是不可变的,并且没有数组函数。如果你能解释一下你想用模板做什么,也许我们可以提供更好的解决方法。你是使用XSLT 1.0还是2.0?不幸的是XSLT 1.0..XSLT变量是不可变的,并且没有数组函数。如果你能解释一下你想用这个模板做什么,我们也许能提供一个更好的解决方法。非常感谢你的时间!最好的解释!非常感谢您抽出时间!最好的解释!