CFLib.org – Common Function Library Project

xsdValidate(xmlPath, noNamespaceXsdUri, namespaceXsdUri, parseError)

Last updated September 23, 2004

author

Samuel Neff

Version: 1 | Requires: CF6 | Library: DataManipulationLib

Description:
This UDF accepts a single XML file any one or more XSD files and validates the XML against the specified XSD(s). Supports no-namespace validation and namespace-aware validation. Returns true/false if valid and optionally a structure with the detailed error message if the XML file doesn't validate properly. Requires that the Xerces Java 2 parser be installed on the CFMX server. http://xml.apache.org/xerces2-j/index.html The file paths must be specified as valid URI's. The UDF makeUriFromPath can be used to convert absolute paths to URI's.

Return Values:
Returns a boolean.

Example:

<cfset err = structNew()>

<cfset xmlUri = "file:///c:/test/bad.xml">
<cfset xsdUri = "file:///c:/test/test.xsd">

<cfoutput>
Valid: #xsdValidate(xmlUri, xsdUri, "", err)#<br />
</cfoutput>

<cfdump var="#err#" label="Information about the error, if any">

Parameters:

Name Description Required
xmlPath Path to XML file. Yes
noNamespaceXsdUri Path to XML Schema file. Yes
namespaceXsdUri Name space. Yes
parseError Struct to contain error information. Yes

Full UDF Source:

<!---
 Validates an XML file against an XML Schema (XSD).
 
 @param xmlPath      Path to XML file. (Required)
 @param noNamespaceXsdUri      Path to XML Schema file. (Required)
 @param namespaceXsdUri      Name space. (Required)
 @param parseError      Struct to contain error information. (Required)
 @return Returns a boolean. 
 @author Samuel Neff (sam@blinex.com) 
 @version 1, April 14, 2005 
--->
<cffunction name="xsdValidate" returnType="boolean" output="false">
  <cfargument name="xmlPath" type="string">
  <cfargument name="noNamespaceXsdUri" type="string">
  <cfargument name="namespaceXsdUri" type="string">
  <cfargument name="parseError" type="struct">
  
  <cfscript>
    var parser = createObject("java","org.apache.xerces.parsers.SAXParser");
    
    var err = structNew();
    var k = "";
    var success = true;
    
    var eHandler = createObject(
                     "java",
                     "org.apache.xml.utils.DefaultErrorHandler");
    
    var apFeat = "http://apache.org/xml/features/";
    var apProp = "http://apache.org/xml/properties/";
    
    eHandler.init();
    
    if (structKeyExists(arguments, "parseError")) {
       err = arguments.parseError;
     }
    
    
    try {
       parser.setErrorHandler(eHandler);
       
       parser.setFeature(
          "http://xml.org/sax/features/validation", 
          true);
          
       parser.setFeature(
          apFeat & "validation/schema", 
          true);
          
       parser.setFeature(
          apFeat & "validation/schema-full-checking", 
          true);
       
       if (structKeyExists(arguments, "noNamespaceXsdUri") and 
           arguments.noNamespaceXsdUri neq "") {
          
          parser.setProperty(
            apProp & "schema/external-noNamespaceSchemaLocation",
            arguments.noNamespaceXsdUri
          
          );
        }
       
       if (structKeyExists(arguments, "namespaceXsdUri") and 
           arguments.namespaceXsdUri neq "") {
          
          parser.setProperty(
            apProp & "schema/external-schemaLocation",
            arguments.namespaceXsdUri
          );
        }
       
       
       parser.parse(arguments.xmlPath);
     } catch (Any ex) {
       structAppend(err, ex, true);
       success = false;
     }
  </cfscript>

  <cfreturn success>
  
</cffunction>

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson