XSLT Concatenation of field names + parameter value for accessing dynamic variable names

We have an XML file that we are trying to figure out how to use dynamically.

the basics are:

<part> <...> <fldMinPriceUSD>100.00</fldMinPriceUSD> <fldMaxPriceUSD>110.00</fldMaxPriceUSD> <fldMinPriceCAD>120.00</fldMinPriceCAD> <fldMaxPriceCAD>130.00</fldMaxPriceCAD> </part> 

for each part that we have, we want to use xslt on it to capture the price of the part based on the currency sent through the parameter. We do not want to use if-elses, because we can increase the list of currencies (EUR, GBP, etc.) without changing our templates.

So, we would like to use the $dealerCurrency parameter (which would be USD, CAD, etc.) to match with fldMinPrice to capture this value. Is this even possible? I tried a few things, but no one works.

What I have tried so far:

 <xsl:value-of select="format-number(str[@name=concat('fldMinPrice', $dealerCurrency)], '#.00')"/> 

and this does not seem to work. Any suggestions?

+4
source share
2 answers

You are almost there. At the moment, using str and @name , you are looking for an element named str that has the attribute name strong> with the value 'fldMinPriceUSD'. You need the local-name () function as well as node () to match any node.

 <xsl:value-of select="format-number(node()[local-name()=concat('fldMinPrice', $dealerCurrency)], '#.00')"/> 

i.e. Match any node with the name (excluding namespaces) of 'fldMinPrice' + your currency code.

+3
source

You guys are geniuses. So, I was a little mistaken in my initial assessment. Our actual XML looks like this (I typed another part from memory ... and I should have some corrupted memory somewhere ... need to replace this):

 <double name="fldMaxPrice">20.0</double> <double name="fldMaxPriceCAD">19.0</double> <double name="fldMinPrice">18.0</double> <double name="fldMinPriceCAD">17.1</double> 

So Tim C ... your answer was perfect ... for the other part of XML with which I tried to do the same. empo, your example of what I was actually looking for helped me to notice that my original post was ALMOST correct, except that I had a street where I had to have a double. Changed str for double and voila!

So, I owe you guys a beer (if you do not want to share, in which case I will take you each of you). :) THANKS!

0
source

All Articles