如何在xsl:for-each中应用模板

时间:2015-06-08 08:22:14

标签: xml xslt xslt-2.0

考虑这个xml结构:

<Lists>
<PriceList Lab="50|51|03|21">
        <action order="11" type="1" />
        <action order="12" type="2" />
</PriceList>
<PriceList Lab="100">
        <action order="13" type="3" />
        <action order="14" type="4" />
</PriceList>
</Lists>

实验室使用tokenize()进行拆分,然后应该应用每个实验操作。 我尝试使用for-each,然后使用选择动作标签。但它不起作用, editix说“Axis step child :: element(action,xs:anyType)不能在这里使用:上下文项是一个原子值”。这是xslt。

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="action">
    <xsl:value-of select="@order"/>
    </xsl:template>

    <xsl:template match="PriceList[@Lab]">
        <xsl:for-each select="tokenize(@Lab, '\|')">
          <xsl:value-of  select="."  />
        <xsl:apply-templates select="action" />
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="/">
        <xsl:apply-templates />
    </xsl:template>
</xsl:stylesheet>

我可以做些什么来完成这项工作?

1 个答案:

答案 0 :(得分:1)

您需要在for-each之外定义一个变量:

<xsl:template match="PriceList[@Lab]">
    <xsl:variable name="this" select="."/>
    <xsl:for-each select="tokenize(@Lab, '\|')">
      <xsl:value-of  select="."  />
      <xsl:apply-templates select="$this/action" />
    </xsl:for-each>
</xsl:template>