xsl-list
[Top] [All Lists]

Re: [xsl] Passing parameter into tag name

2017-04-12 11:18:55
My preference when I see poorly-designed XML like this is to start by writing a 
transformation that turns it into well-designed XML, in this case converting

<Level_1_Amt>100</Level_1_Amt>

to

<Amt level="1">100</Amt>

The great advantage of this is that you only need to do it once; from then on 
you can work with your easier-to-manipulate XML.

Michael Kay
Saxonica

On 12 Apr 2017, at 14:06, Steve Wisniewski stevewiz76(_at_)yahoo(_dot_)com 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Hello all,

I have a data scenario like this (edited down for clarity) where I have 
multiple tags that have the actual level number in the tag name:

<foo>
      <Level>
              <Level_No>1</Level_No>
      </Level>
      <Level>
              <Level_No>2</Level_No>
      </Level>
      <Level>
              <Level_No>3</Level_No>
      </Level>
      <Cost ID='1'>
                      <Level_1_Amt>100</Level_1_Amt>
                      <Level_2_Amt>200</Level_2_Amt>
                      <Level_3_Amt>300</Level_3_Amt>
      </Cost>
      <Cost ID='2'>
                      <Level_1_Amt>50</Level_1_Amt>
                      <Level_2_Amt>100</Level_2_Amt>
                      <Level_3_Amt>150</Level_3_Amt>
      </Cost>
      <Cost ID='3'>
                      <Level_1_Amt>175</Level_1_Amt>
      </Cost>
      <Cost ID='4'>
                      <Level_3_Amt>400</Level_3_Amt>
      </Cost>
</foo>

I am trying to sum up the amounts in the following code, but am getting a 
syntactical error about how I am trying to pass the parameter value into the 
tag name:

<xsl:stylesheet version="2.0" xmlns="http://www.w3.org/1999/xhtml"; 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
xmlns:xs="http://www.w3.org/2001/XMLSchema";>
      <xsl:output method="xml" indent="yes"/>

      <xsl:template name="buildLevel">
              <xsl:param name="levelNum"/>
              <Level_Amt>
                      <xsl:value-of 
select="sum(/Story/Level_{$levelNum}_Amt)"/>
              </Level_Amt>
      </xsl:template>

      <xsl:template match="Cost">
              <xsl:for-each select="/foo/Level">
                      <Level>
                              <xsl:call-template name="buildLevel">
                                      <xsl:with-param name="levelNum" 
select="Level_No"/>
                              </xsl:call-template>
                      </Level>
              </xsl:for-each>
      </xsl:template>
      
</xsl:stylesheet>

Is there a way I can do something like this? I am trying to avoid explicitly 
calling each tag by name since there could be up to 9 levels in the data and 
there are multiple tags that have the level number in them. Thank you in 
advance. 
XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <-list/293509> (by email <>)
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>