Monday, April 25, 2011

XSLT foreach

Hi i have a strange issue with matching specific attribute of xml node. Example code that doesnt work:

<xsl:for-each select="../../unit/service/price/season[@name=$period_name]">
     <xsl:attribute name="std_bed_price">
          <xsl:value-of select="../@amount"/>
     </xsl:attribute>
</xsl:for-each>

Example code that DOES work but i don't like this way too much:

 <xsl:for-each select="../../unit/service/price/season">
     <xsl:if test="@name = $period_name">
          <xsl:attribute name="std_bed_price">
               <xsl:value-of select="../@amount"/>
          </xsl:attribute>
     </xsl:if>
 </xsl:for-each>

If in first example i replace the variable name with some of the values like 'A' it works, i also tested what variable name is selected and it has the correct data inside (so, 'A','B','C' ...)

Anyone had this problem before?

Tnx

From stackoverflow
  • Not seen this before. Could it be an ambiguous @name attribute. Therefore try accessing it like below?

    select="../../unit/service/price/season[./@name=$period_name]
    

    Other than that, sorry, to me it looks like it should work perfectly both ways.

  • You might try changing it to an apply-templates instead of a foreach. Something like the following should work.

    <xsl:template match="price">
        <xsl:attribute name="std_bed_price">
            <xsl:value-of select="@amount" />
        </xsl:attribute>
    </xsl:template>
    

    And then call it like:

    <xsl:apply-template select="../../unit/service/price/[season/@name=$period_name]" />
    

0 comments:

Post a Comment