Search code examples
xmlxsltxsl-foapache-fop

how to get variable value from xml file in xslt


I have xml file in which having some css properties and I want to apply on table

XML file is as follows :-

   <code>
     <reportConfiguration>
        <details_background_color>white</details_background_color>  
        <page_header_horizontal_align>center</page_header_horizontal_align>
        <page_header_font_size>12pt</page_header_font_size>
     </reportConfiguration>
   </code>

I want use details_background_color , details_bold to apply value in table , code as follows but not working

<code>
  <fo:block>
        <xsl:for-each select="element_1">
         <fo:block  font-size="document('xmlFile_reportConfig.xml')/reportConfiguration/page_header_font_size" font-weight="document('xmlFile_reportConfig.xml')/reportConfiguration/details_bold" text-align="document('xmlFile_reportConfig.xml')/reportConfiguration/page_header_horizontal_align"  vertical-align="middle">
                            select="document('file:///D:/DATA/Marquee/dial_stats_UK.xml')/UK_Products_Pipeline/LastFinishCode"
                                <xsl:value-of select="."/>
                            </fo:block>
                        </xsl:for-each>
                    </fo:block>
</code>

Solution

  • I am assuming the XML file you have shown us is not the primary document you are applying the XSLT too, but a secondary document you need to reference in addition to the main one, otherwise there would be no need in using the document function.

    (I am also going to assume the <code> tags showing in your question should also not be there).

    Anyway, this is how you are currently trying to reference the external document

    <fo:block 
        font-size="document('xmlFile_reportConfig.xml')/reportConfiguration/page_header_font_size"
    

    When you try to set the value of an attribute using an expression, you need to use Attribute Value Templates, otherwise the value of the attribute will be literally what you write. In other words, you need to enclose the expression in curly braces to indicate it is an expression to be evaluated:

    <fo:block 
        font-size="{document('xmlFile_reportConfig.xml')/reportConfiguration/page_header_font_size}"
    

    It might be slightly easier to hold a reference to the document in a variable though, if you need to access multiple values from it. For example:

    <xsl:variable name="config" select="document('xmlFile_reportConfig.xml')/reportConfiguration" />
    <fo:block font-size="{$config/page_header_font_size}" font-weight="{$config/details_bold}" />
    

    Note, this does assume your report configuration XML is in the same directory as the XSLT file.