How does one do a dynamic value-of?
This is the case:
1) We get an XML-export from system A.
2) We select and format some of the data which is needed by system B.
3) One of the passed fields needs to be combined from multiple elements of the source xml.
The combined elements can be customized by the customer.
So, what I have now is this:
XSLT for transformation of the xml from system A
A simple configuration xml where the customer can specify which fields he wants to combine in a sequential way:
<items>
<field type="field">field1</field>
<field type="text">whatever the customer wants between the combined fields</field>
<field type="field">field2</field>
<field type="text">whatever the customer wants between/after the combined fields</field>
<field type="field">field3</field>
</items>
an example of this xml would translate to:
<field1>, <field2> and <field3> or <field1>+<field2>=<field3>
In my xslt I have this:
<xsl:variable name="configfile" select="document(myconfigxml.xml)"/>
Then I use xslt to loop over the field types and where the @type=’field’ I want to use the value of the field to look into the element of the source xml.
<xsl:template name="items">
<xsl:param name="personElementFromSourceXml"/>
<xsl:for-each select="$configfile/items/field">
<xsl:choose>
<xsl:when test="@type='field'">
<xsl:value-of select="???"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
What would I need to type on the place of the ??? ? In pseudo-xslt it would be $personElementFromSourceXml/value-of-<field1-2-3>
I tried a number of things, such as concat($personElementFromSourceXml, ‘/’, current()), tried $personElementFromSourceXml/current(), but they don’t work.
Also, small sanity check here: Is my idea of tackling this problem an okay approach or am I violating every rule in the book? Note, I’m still new to xslt.
You’ve got the right idea, namely selecting the element whose content you by means of the value of its name. Forgive me if I misunderstood your question, but I interpreted the ‘items’ file as a template file and assumed a separate input file. E.g. for the config file:
and for the source file:
I adapted your code thus:
Because the
xsl:for-eachis iterating over$configfile/items/field, eachfieldelement becomes the context node, so you need a way of explicitly referring to the source file, which is why I assign it to a global variable. I get this output: