Nested for-each loops, accessing outer element with variable from the inner loop

后端 未结 1 625
北海茫月
北海茫月 2021-01-01 22:53

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

相关标签:
1条回答
  • 2021-01-01 23:05

    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]"/>
    
    0 讨论(0)
提交回复
热议问题