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>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson