Lets say I have the following xml.
<root>
<data>
<a>ATTITUDE_ANNOYED</a>
<b>ATTITUDE_CAUTIOUS</b>
<c>25</c>
<d>30</d>
</data>
</root>
Ignoring the schema of my output, I want my output to present A as “Cautious” (one level up from annoyed), B as ”
Pleased” (one level up from Cautious”) and I want to perform some maths on C and D to convert the value into something slightly different.
I’ve had a look at a bunch of similar questions here (and I’m new to XSLT so maybe I don’t quite get it) but a lot of the solutions appear to be “in-line”, i.e. you modify the result as you transform it. This is okay but in my real example there are a lot of these values and I don’t want to be performing the exact same conversion in multiple places (DRY). I just want to more or less pre-process the entire document and convert a bunch of values into other values (using just a few formulas) before I start the transformation.
What would be the best way to achieve this? I’m not particularly interested in performance so is there a way that I can run a prior transformation to easily transform specific values without modifying the structure?
UPDATE: (DevNull asked for my output desires) The output isn’t exactly finalised. I’m trying to help a group at CivFanatics produce a guide on the differences between the AIs whose values derive from an xml file. There are a ton of leaders and a ton of values that need converting, by hand its taking them 2 hours per leader at the moment and the final formatting hasn’t been decided on so I thought we’d all save time by using something like XLST.
Here is a rough example of a demo I’m working on.
<xsl:template match="data">
<h3>Attitude Thresholds</h3>
<table border="1">
<tr><td>Will open borders</td><td><xsl:value-of select="a"/></td></tr>
<tr><td>Will trade techs</td><td><xsl:value-of select="b"/></td></tr>
</table>
</xsl:template>
To clarify A and B are OpenBordersRefuseAttitudeThreshold and TechRefuseAttitudeThreshold. The guide is more readable if these are WillOpenBordersAt and WillTradeTechAt as opposed to the original values in the xml file so I need to nudge them up a value for the final output.
There are additional conversions that have been discussed:
iWonderConstructRand -> Builds Wonders
0 -> 0/10
5 -> 1/10
10 -> 2/10
15 -> 3/10
20 -> 4/10
25 -> 5/10
30 -> 6/10
35 -> 7/10
40 -> 8/10
45 -> 9/10
50 -> 10/10
So that’s values such as C or D. They range between 0-50 and should be put into a 0-10 format for readability. There are a fair few of these values too.
Also there are conversions like “GoodieBaddie” which vary from 0 to 10. Which we wish to convert to something like:
0-3 -> Bad(x)
4-6 -> Neutral(x)
7-10 -> Good(x)
Where (x) is the original value.
Am I even using the right tool for the job here or is it borderline? I figured XLST would be a good choice to enable other contributors to not have to rely on the devs to make changes to the formatting/layout (as xlst is easier to edit then say C# or Python).
This transformation:
when applied on the provided XML document:
produces the wanted, correct result: