How do I diagnose and resolve "Could not find variable with the name of XYZ" XSL errors?

时间:2017-03-22 18:42:20

标签: xml xslt xslt-1.0

When I attempt to validate the following XLS document using http://www.utilities-online.info/xsltransformation, the validator replies with:

Not valid. FatalError: javax.xml.transform.TransformerException: org.apache.xml.utils.WrappedRuntimeException: Could not find variable with the name of detailReport

I have examined the XLS document, but the problem is not readily apparent to me. What is wrong with the XLS, and how can I correct it?

XLS:

<!-- CREATED: CowieML -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:CSD-Namespace" version="1.0" >
    <!-- Declares a namespace and associates it with the script block. -->
    <xsl:output method="html"/>  
    <!-- Defines external look-up XML files -->
    <xsl:key name="err-cd" match="errormsg" use="@CD"/>
    <xsl:variable name="err-lookup" select="document('/webroot/xsl/ERROR-LOOKUP.XML')"/>
    <xsl:template match="/">
        <HTML>
            <HEAD>
                <link href="/webroot/webroot.css" rel="stylesheet" title="webroot"></link>
                <STYLE>
                    BODY { BACKGROUND-ATTACHMENT: fixed ; font-family: Arial;color:black; margin-right:0; }
                    DIV { font-family: Arial, Helvetica; color:black; font-size:10pt; }
                    I { font-family: Arial, Helvetica; color:black; font-size:8pt; line-height:10pt;  }
                    .Outline { font-size:10pt; line-height:10pt; color:black; text-decoration:none;  }
                    .Subject { font-size:10pt; line-height:10pt; color:black;  text-decoration:none;  }
                    .Outline2 { font-size:10pt; line-height:10pt; color:black;  text-decoration:none; }
                    .Detail { font-size:10pt; line-height:10pt; color:black; text-decoration:none; }
                    .topic   { font-size:12pt; line-height:10pt; color:black; text-decoration:underline; font-weight:bold; }
                    .textlbl { font-size:8pt;  line-height:10pt; color:black; text-decoration:none;      font-weight:bold; }
                    .text    { font-size:8pt;  line-height:10pt; color:black; text-decoration:none; }
                </STYLE>
            </HEAD>
            <xsl:apply-templates/>
        </HTML>
    </xsl:template>

    <!--                              -->
    <!-- BEGIN websiteHEADER              -->
    <!--                              -->
    <xsl:template match="websiteHEADER">
        <BODY>
            <TABLE  width="100%" cellspacing="2" cellpadding="2" border="0">
                <TR>
                    <TD ROWSPAN="3"><IMG SRC="/img/seal.jpg" ALT="Seal"  border="0"></IMG></TD>
                    <TD class="report_header0">DEPARTMENT</TD>
                </TR>
                <TR>
                    <TD COLSPAN="2" class="report_header1"><xsl:value-of select="@TITLE"/></TD>
                </TR>
                <TR>
                 <TD COLSPAN="2" class="report_header3">Add requested by <xsl:value-of select="@REQUSER"/> on <xsl:value-of select="@REQDATE"/></TD>
                </TR>
                <xsl:variable name="detailReport">
                    <xsl:value-of select="@DETAIL"/>
                </xsl:variable>
            </TABLE>
            <TABLE width="100%" cellspacing="2" cellpadding="2" border="0">
                <xsl:for-each select="BATCH/ITEMLIST/ITEM/VISAST">
                    <xsl:variable name="status" select="(@COD)"/>
                    <xsl:if test='($status!="E00")'>
                        <TR>
                            <TD colspan="3">
                                <B>
                                    TCM or CLASS returned the following error. 
                                    <br/>If this issue persists, please contact the Helpdesk at 
                                    <a>
                                        <xsl:attribute name="href">
                                            mailto:help@company.com?subject=An error occured while submitting. Error Code:
                                            <xsl:value-of select="@COD"/>
                                            . BATCH ID:
                                            <xsl:value-of select="/websiteHEADER/BATCH/@ID"/>
                                            . SIT:
                                            <xsl:value-of select="/websiteHEADER/BATCH/@SIT"/>
                                            . TYP: VISARP.
                                        </xsl:attribute>
                                        help@company.com                                                            
                                    </a>
                                    with this error.
                                </B>
                            </TD>
                            <TD class="text"> </TD>
                        </TR>
                        <xsl:for-each select="$err-lookup">
                            <xsl:variable name="err" select="key('err-cd', $status)"/>
                            <TR>
                                <TD class="textlbl">
                                    <br/>
                                    <B>Error Code: </B>
                                </TD>
                                <TD class="text">
                                    <br/>
                                    <xsl:value-of select="$err/@CD"/>
                                </TD>
                                <TD class="text"> </TD>
                            </TR>
                            <TR>
                                <TD class="textlbl">
                                    <B>Error Description:</B>
                                </TD>
                                <TD class="text">
                                    <xsl:value-of select="$err/@DESC"/>
                                </TD>
                                <TD class="text"> </TD>
                            </TR>
                        </xsl:for-each>
                    </xsl:if>
                    <xsl:if test='($status="E00")'>
                        <TR>
                            <TD class="topic">The following subject has been added.</TD>
                        </TR>
                        <TR>
                            <TD class="Detail">Click on the subject's name to submit a check.</TD>
                        </TR>
                        <TR>
                            <TD> </TD>
                        </TR>
                        <TR>
                            <TD>
                                <TABLE style="height:10; width:100%" >
                                    <xsl:for-each select="../VISADD">
                                        <xsl:variable name="birth-date-type" select="(@DBT)"/>
                                        <xsl:variable name="birth-date" select="(@DOB)"/>
                                        <xsl:variable name="nationality-len" select="count(@NAT)"/>
                                        <xsl:variable name="nationalid-len" select="count(@NID)"/>
                                        <xsl:variable name="gender-len" select="count(@G)"/>
                                        <TR>
                                            <TD class="textlbl"> Name: </TD> 
                                            <TD class="text">
                                                <xsl:variable name="aamp"><![CDATA[&]]></xsl:variable>
                                                <a>
                                                    <xsl:attribute name="href">
                                                        <xsl:copy-of select="$detailReport"/>?p_surname=<xsl:value-of select="(@SN)"/><xsl:copy-of select="$aamp"/>p_given_name=<xsl:value-of select="(@GN)"/><xsl:copy-of select="$aamp"/>p_birth_place_country_entry=<xsl:value-of select="(@COB)"/><xsl:copy-of select="$aamp"/>p_birth_place_country_cd=<xsl:value-of select="(@COB)"/>
                                                        <xsl:if test='($birth-date-type="000")'><xsl:copy-of select="$aamp"/>p_birth_day=<xsl:value-of select="user:fmtDD($birth-date)"/><xsl:copy-of select="$aamp"/>p_birth_month=<xsl:value-of select="user:fmtMON($birth-date)"/><xsl:copy-of select="$aamp"/>p_birth_year=<xsl:value-of select="user:fmtYYYY($birth-date)"/></xsl:if>
                                                        <xsl:if test='($birth-date-type="001")'><xsl:copy-of select="$aamp"/>p_birth_month=<xsl:value-of select="user:fmtMON($birth-date)"/><xsl:copy-of select="$aamp"/>p_birth_year=<xsl:value-of select="user:fmtYYYY($birth-date)"/></xsl:if>
                                                        <xsl:if test='($birth-date-type="011")'><xsl:copy-of select="$aamp"/>p_birth_year=<xsl:value-of select="user:fmtYYYY($birth-date)"/></xsl:if>
                                                        <xsl:if test='not($nationality-len=0)'><xsl:copy-of select="$aamp"/>p_nationality_cd=<xsl:value-of select="(@NAT)"/><xsl:copy-of select="$aamp"/>p_nationality_entry=<xsl:value-of select="(@NAT)"/></xsl:if>
                                                        <xsl:if test='not($nationalid-len=0)'><xsl:copy-of select="$aamp"/>p_national_id=<xsl:value-of select="(@NID)"/></xsl:if>
                                                        <xsl:if test='not($gender-len=0)'><xsl:copy-of select="$aamp"/>p_gender=<xsl:value-of select="(@G)"/></xsl:if>
                                                    </xsl:attribute>
                                                    <xsl:attribute name="target">_blank</xsl:attribute>
                                                    <xsl:value-of select="(@SN)"/>, <xsl:value-of select="(@GN)"/>
                                                </a>
                                            </TD>
                                            <TD class="text"> </TD>
                                        </TR>
                                        <TR>
                                            <TD class="textlbl"> Country of Birth: </TD> 
                                            <TD class="text"><xsl:value-of select="(@COB)"/></TD>
                                        </TR>
                                        <TR>
                                            <TD class="textlbl"> Nationality: </TD> 
                                            <TD class="text"><xsl:value-of select="(@NAT)"/></TD>
                                        </TR>
                                        <TR>
                                            <TD class="textlbl"> Date of Birth: </TD>
                                            <TD class="text">

                                                <xsl:if test='($birth-date-type="211")'>
                                                    <xsl:value-of select="user:fmtYYYY($birth-date)"/> (Estimated) 
                                                </xsl:if>

                                                <xsl:if test='($birth-date-type="011")'>
                                                    <xsl:value-of select="user:fmtYYYY($birth-date)"/>
                                                </xsl:if>

                                                <xsl:if test='($birth-date-type="001")'>
                                                    <xsl:value-of select="user:fmtMONYYYY($birth-date)"/>
                                                </xsl:if>

                                                <xsl:if test='($birth-date-type="000")'>
                                                    <xsl:value-of select="user:fmtDDMONYYYY($birth-date)"/>
                                                </xsl:if>
                                            </TD>
                                        </TR>
                                        <!-- 2017-02-07 leei: CR 1011246 Transformation on Gender - dislay only when value exists -->
                                        <xsl:variable name="gender-code" select="(@G)"/>                    
                                        <xsl:if test='($gender-code="F")'>                                           
                                            <TR>                                                                    
                                                <TD class="textlbl"> Gender: </TD>        
                                                <TD class="text">Female</TD>                                   
                                            </TR>                                                                                             
                                        </xsl:if>                                                                    
                                        <xsl:if test='($gender-code="M")'>                                           
                                            <TR>                                                                    
                                                <TD class="textlbl"> Gender: </TD>        
                                                <TD class="text">Male</TD>                                     
                                            </TR>                                                                                             
                                        </xsl:if>                                                                    
                                        <xsl:if test='($gender-code="U")'>                                           
                                            <TR>                                                                    
                                                <TD class="textlbl"> Gender: </TD>        
                                                <TD class="text">Unknown</TD>                                     
                                            </TR>                                                                                             
                                        </xsl:if>                                                                    
                                        <TR>
                                            <TD class="textlbl"> National ID: </TD> 
                                            <TD class="text"><xsl:value-of select="(@NID)"/></TD>
                                        </TR>
                                        <TR>
                                            <TD class="textlbl"> Passport Number: </TD> 
                                            <TD class="text"><xsl:value-of select="(@PPT)"/></TD>
                                        </TR>
                                        <TR>
                                            <TD class="textlbl"> Short Comment: </TD> 
                                            <TD class="text"><xsl:value-of select="(@SCOM)"/></TD>
                                        </TR>  
                                        <TR>
                                            <TD class="textlbl"> Refusal Date: </TD>
                                            <TD class="text">
                                                <xsl:variable name="refusal-date" select="(@REFDATE)"/>
                                                <xsl:value-of select="user:fmtDDMONYYYY($refusal-date)"/>
                                            </TD>
                                        </TR>
                                        <TR>
                                            <TD class="textlbl"> Post Code: </TD> 
                                            <TD class="text"><xsl:value-of select="(@REFSITE)"/></TD>
                                        </TR>
                                    </xsl:for-each> <!-- VISADD -->
                                </TABLE>  
                            </TD> 
                        </TR>
                        <TR>
                            <TD>
                                <TABLE style="height:10; width:100%" >
                                    <xsl:for-each select="../VISADD/REFITEMLIST/REFITEM">
                                    <TR>
                                        <TD class="textlbl"> Refusal Code: </TD> 
                                        <TD class="text"><xsl:value-of select="(@REFCODE)"/></TD>
                                        <TD class="text"> </TD>
                                    </TR>
                                    </xsl:for-each> <!-- REFITEM -->
                                </TABLE>  
                            </TD> 
                        </TR>
                        <TR>
                            <TD>
                                <TABLE style="height:10; width:100%" >
                                    <xsl:for-each select="../VISADD/OTHERNAMELIST/OTHERNAME">
                                        <TR>
                                            <TD class="textlbl"> Alias: </TD> 
                                            <TD class="text"><xsl:value-of select="(@SEQ)"/></TD>
                                            <TD class="text"> </TD>
                                        </TR>
                                        <TR>
                                            <TD class="textlbl"> Surname: </TD> 
                                            <TD class="text"><xsl:value-of select="(@SN)"/></TD>
                                        </TR>
                                        <TR>
                                            <TD class="textlbl"> Given Name: </TD> 
                                            <TD class="text"><xsl:value-of select="(@GN)"/></TD>
                                        </TR>
                                    </xsl:for-each> <!-- OTHERNAME -->
                                </TABLE>  
                            </TD> 
                        </TR>
                    </xsl:if>
                </xsl:for-each>
            </TABLE> 
        </BODY>
    </xsl:template>
    <!--               -->
    <!-- BEGIN JSCRIPT -->
    <!--               -->
    <msxsl:script language="JScript" implements-prefix="user">
        <![CDATA[

        function fmtDDMONYYYY(nodelistx) {

            var s = nodelistx.item(0).text;
            var mm = s.substr(5,2);

            if (mm == '01')
            ms = 'JAN';
            else if (mm == '02')
            ms = 'FEB';
            else if (mm == '03')
            ms = 'MAR';       
            else if (mm == '04')
            ms = 'APR';
            else if (mm == '05')
            ms = 'MAY';
            else if (mm == '06')
            ms = 'JUN';       
            else if (mm == '07')
            ms = 'JUL';   
            else if (mm == '08')
            ms = 'AUG';
            else if (mm == '09')
            ms = 'SEP';       
            else if (mm == '10')
            ms = 'OCT';   
            else if (mm == '11')
            ms = 'NOV';       
            else if (mm == '12')
            ms = 'DEC';   
            else       
            ms = '   ';


            if (mm == '00') 
            return ' ';
            else
            return s.substr(8,2)+'-'+ms+'-'+s.substr(0,4);
        }

        function fmtMONYYYY(nodelistx) {

            var s = nodelistx.item(0).text;                                  
            var mm = s.substr(5,2);

            if (mm == '01')
            ms = 'JAN';
            else if (mm == '02')
            ms = 'FEB';
            else if (mm == '03')
            ms = 'MAR';       
            else if (mm == '04')
            ms = 'APR';
            else if (mm == '05')
            ms = 'MAY';
            else if (mm == '06')
            ms = 'JUN';       
            else if (mm == '07')
            ms = 'JUL';   
            else if (mm == '08')
            ms = 'AUG';
            else if (mm == '09')
            ms = 'SEP';       
            else if (mm == '10')
            ms = 'OCT';   
            else if (mm == '11')
            ms = 'NOV';       
            else if (mm == '12')
            ms = 'DEC';   
            else       
            ms = '   ';


            if (mm == '00') 
            return ' ';
            else
            return ms+'-'+s.substr(0,4);
        }
        function fmtDD(nodelistx) {

            var s = nodelistx.item(0).text;                               
            return s.substr(8,2);
        }

        function fmtMON(nodelistx) {

            var s = nodelistx.item(0).text;                               
            var mm = s.substr(5,2);

            if (mm == '01')
            ms = 'JAN';
            else if (mm == '02')
            ms = 'FEB';
            else if (mm == '03')
            ms = 'MAR';       
            else if (mm == '04')
            ms = 'APR';
            else if (mm == '05')
            ms = 'MAY';
            else if (mm == '06')
            ms = 'JUN';       
            else if (mm == '07')
            ms = 'JUL';   
            else if (mm == '08')
            ms = 'AUG';
            else if (mm == '09')
            ms = 'SEP';       
            else if (mm == '10')
            ms = 'OCT';   
            else if (mm == '11')
            ms = 'NOV';       
            else if (mm == '12')
            ms = 'DEC';   
            else       
            ms = '   ';


            if (mm == '00') 
            return ' ';
            else
            return ms;
        }

        function fmtYYYY(nodelistx) {

            var s = nodelistx.item(0).text;                               
            return s.substr(0,4);
        }

        ]]>
    </msxsl:script>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

变量是声明它们的代码块的本地变量,并且您已将变量声明为<TABLE>的子项

<TABLE  width="100%" cellspacing="2" cellpadding="2" border="0">
    <!-- snip -->
     <xsl:variable name="detailReport">
         <xsl:value-of select="@DETAIL"/>
     </xsl:variable>
</TABLE>

这意味着变量只能在TABLE元素中访问,而不能在模板中进一步使用。

在这种情况下,解决方案是将变量声明向上移动到TABLE标记之外,以便在模板中的任何位置都可以访问它。在模板匹配之后应该这样做..

<xsl:template match="websiteHEADER">
    <xsl:variable name="detailReport">
        <xsl:value-of select="@DETAIL"/>
    </xsl:variable>      
    <BODY>