XSLT Concatenation of fieldnames + parameter value for Dynamic Variable Name access
We have an XML file that we are trying to figure out a way to use dynamically.
the basics are this:
<part>
<...>
<fldMinPriceUSD>100.00</fldMinPriceUSD>
<fldMaxPriceUSD>110.00</fldMaxPriceUSD>
<fldMinPriceCAD>120.00</fldMinPriceCAD>
<fldMaxPriceCAD>130.00</fldMaxPriceCAD>
</part>
for each part that we've got, we want to use xslt on it to grab the part price based on the currency sent in through a parameter. We don't want to use if-elses because we might want to grow the list of currencies (EUR, GBP, etc.) without changing our templates.
So, we'd want to use the $dealerCurrency
parameter (that would be USD, CAD, etc.) to concat with fldMinPrice to grab that value. Is this sort of thing even possible? I've tried a number of things, but none seem to work.
What I've tried so far is this:
<xsl开发者_如何学JAVA:value-of select="format-number(str[@name=concat('fldMinPrice', $dealerCurrency)], '#.00')"/>
and this doesn't seem to work. Any suggestions?
You are almost there. At the moment, by using str and @name you are looking for an element called str which has an attribute called name with a value of 'fldMinPriceUSD'. What you need is the local-name() function, along with node() to match any node.
<xsl:value-of select="format-number(node()[local-name()=concat('fldMinPrice', $dealerCurrency)], '#.00')"/>
i.e. Match any node, with a name (excluding namespaces) of 'fldMinPrice' + your currency code.
You guys are geniuses. So, I was a little wrong in my original assessment. Our actual XML looks like this (I had typed in the other part from memory... and I must have some corrupt memory somewhere... gotta get that replaced):
<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 another part of the XML I was trying to do the same thing with. empo, your example of what I was actually looking for helped me notice that my original post was ALMOST correct, except that I had a str where I should have had a double. Changed str to double and voila!
So, I owe you guys a beer (unless you don't want to share, in which case, I'll get you each your own). :) THANKS!
精彩评论