将xslt从1.0升级到2.0并获取javax.xml.transform.TransformerException:找不到函数:匹配

时间:2017-08-29 16:59:02

标签: xml xslt

我刚刚将xsl从1.0升级到2.0,因为我想利用matches。我认为这就像更改版本号一样简单,但我得到例外:

javax.xml.transform.TransformerConfigurationException: 
    javax.xml.transform.TransformerConfigurationException: 
        javax.xml.transform.TransformerException: 
            org.xml.sax.SAXException: Could not find function: matches
javax.xml.transform.TransformerException: 
    Could not find function: matches

这就是我的代码执行xml验证的方式:

import java.io.ByteArrayOutputStream;
import java.util.StringTokenizer;    
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
. . . 

private String validate_theXml_using_theXsl(org.w3c.dom.Document docXml, String Xslfilename) //xslfilename: D:\xml\templates\funding.xsl
{
        String err= new String();

    try{

        DOMSource xmlDomSource = new DOMSource(docXml);     
        TransformerFactory factory = TransformerFactory.newInstance();          
        StreamSource stream = new StreamSource(Xslfilename);            
        Transformer mytransformer = factory.newTransformer(stream); // <-- this is where it throws exception       
        mytransformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");                      
        ByteArrayOutputStream xmlOut = new ByteArrayOutputStream();         
        StreamResult result = new StreamResult(xmlOut);        
        mytransformer.transform(xmlDomSource, result);          
        String errcomplete = xmlOut.toString();                   

        //first 45 characters are stripped off because the first line is the xml definition
        err = errcomplete.substring(45);

        if(debug)  { 
            System.out.println("\nerrors:"+err+"\n");
             StreamResult res= new StreamResult(System.out);
            mytransformer.transform(xmlDomSource, res);
        }

    }catch(Exception e ){
        e.printStackTrace();
    }                   
    return err;
}

d:\ XML \模板\ funding.xsl

<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:template match="Funding">
        <xsl:apply-templates select="FundingDocNumber"/>
    </xsl:template>

    <xsl:template match="FundingDocNumber">
    <xsl:if test="string-length(.) &gt; 1">Accounting Number('<xsl:value-of select="string(.)"/>') - may have up to 1 character<br/></xsl:if>
        <xsl:if test="string-length(.) = 0">Accounting Number is mandatory<br/></xsl:if>
        <xsl:if test="string(/Funding/FundsAgency) ='GEN' and not(string(/Funding/FundsFormCode) ='PP')">
            <xsl:if test="string(number(string(.) ) ) ='NaN'">Accounting Number must be numeric <br/> </xsl:if>
        </xsl:if>
        <xsl:if test="not(matches(., '^[a-zA-Z0-9]+$'))">Data entered - Accounting Number('<xsl:value-of select="string(.)"/>') may not have special characters <br/></xsl:if>
    </xsl:template>
</xsl:transform>

有谁知道升级到v2.0需要哪些其他步骤?

0 个答案:

没有答案