how to apply templates within xsl:for-each

670 views Asked by At

Consider this xml structure:

<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>

Lab is splitted with tokenize() and then for every lab action should be applied. I tried using for-each and then use to select the action tags. But it does not work, editix says "Axis step child::element(action, xs:anyType) cannot be used here: the context item is an atomic value". Here is the 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>

What can I do to make this work?

1

There are 1 answers

0
Martin Honnen On BEST ANSWER

You need to define a variable outside of the 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>