I\'m trying to write an XSL that will output a certain subset of fields from the source XML. This subset will be determined at transformation time, by using an external XML
Your stylesheet looks almost fine. Just the expression $rec/$field
doesn't make sense because you can't combine two node sets/sequences this way. Instead, you should compare the names of the elements using the name()
function. If I understood your problem correctly, something like this should work:
<xsl:variable name="config" select="document('./configuration.xml')"/>
<xsl:for-each select="data/dataset/record">
<xsl:variable name="rec" select="."/>
<xsl:for-each select="$config/configuration/outputField">
<xsl:variable name="field" select="fieldName"/>
...
<xsl:variable name="value" select="$rec/*[name(.)=$field]"/>
...
</xsl:for-each>
<xsl:value-of select="$newline"/>
</xsl:for-each>
Variable field is not required in this example. You can also use function current()
to access the current context node of the inner loop:
<xsl:variable name="value" select="$rec/*[name(.)=current()/fieldName]"/>