Home › Forums › General XMetaL Discussion › Well-Formed Check In XMAX › Reply To: Well-Formed Check In XMAX
Reply to: Well-Formed Check In XMAX
November 16, 2011 at 12:19 amOK, I would suggest using a function similar to the following. You could call it from any code you have in place for XMAX that is displaying validation errors and add your own messaging (I have provided some suggestions, though you might also just simplify to something like “your PI target is bad”).
Code for locating the PIs (and their target values) in your document is not provided here as that should be simple to figure out I think.
This function returns a value indicating the “validity” of a particular name as a number value 0-3 where 0 means “valid” and the other three values have slightly different meanings (while they all also mean “invalid”).
The specific rules for PIs are defined here: http://www.w3.org/TR/REC-xml/#sec-pi
My code has adapted the definitions for the character groups that are allowed in the XML Recommendation and turned those into JScript regular expressions to make it easy to match those up and confirm that the right characters are in fact being looked for.
[code]//XMetaL Script Language JScript:
function isValidName(nameStr) {
if (nameStr.substr(0,3).toUpperCase() != “XML”) {
//name doesn't begin with “XML” (case insensitive), continue…
var NameStartCharRE=/([:]|[A-Z]|[_]|[a-z]|[u00C0-u00D6]|[u00D8-u00F6]|[u00F8-u02FF]|[u0370-u037D]|[u037F-u1FFF]|[u200C-u200D]|[u2070-u218F]|[u2C00-u2FEF]|[u3001-uD7FF]|[uF900-uFDCF]|[uFDF0-uFFFD])/;
if (nameStr.charAt(0).search(NameStartCharRE) == 0) {
//first character is OK, continue to check the rest…
var NameCharRE = /([:]|[A-Z]|[_]|[a-z]|[u00C0-u00D6]|[u00D8-u00F6]|[u00F8-u02FF]|[u0370-u037D]|[u037F-u1FFF]|[u200C-u200D]|[u2070-u218F]|[u2C00-u2FEF]|[u3001-uD7FF]|[uF900-uFDCF]|[uFDF0-uFFFD]|[-]|(.)|[0-9]|[u00B7]|[u0300-u036F]|[u203F-u2040])/;
for(i=0;i
return 1; // target contains an illegal character
}
}
//we got through every character without error…
return 0; // target is a valid name
}
else {
return 2; // target begins with an illegal character
}
}
else {
return 3; // target must not begin with the string 'xml', or with any string which would match ((X|x) (M|m) (L|l))
}
}[/code]
Call the following (after defining the function above elsewhere in your code).
These tests simply pass in various strings to test as target names to confirm that my function is testing properly.
[code]//Following should return 2: target begins with an illegal character
var name = “1234”;
var response = isValidName(name);
ActiveDocument.Host.Alert(“target = ” + name + “nn” + response);
//Following should return 3: target must not begin with the string 'xml'
var name = “XmL1234”;
var response = isValidName(name);
ActiveDocument.Host.Alert(“target = ” + name + “nn” + response);
//Following should return 0: target is valid
var name = “_1234”;
var response = isValidName(name);
ActiveDocument.Host.Alert(“target = ” + name + “nn” + response);
//Following should return 0: target is valid
var name = “_1234.asdf”;
var response = isValidName(name);
ActiveDocument.Host.Alert(“target = ” + name + “nn” + response);
//Following should return 0: target is valid
var name = “:1234”;
var response = isValidName(name);
ActiveDocument.Host.Alert(“target = ” + name + “nn” + response);
//Following should return 0: target is valid
var name = “a1234”;
var response = isValidName(name);
ActiveDocument.Host.Alert(“target = ” + name + “nn” + response);
//Following should return 1: target contains an illegal character
//Note that the last character in this string is a 'GREEK QUESTION MARK' (it may look like a standard semicolon).
//If the forum software or database does not properly preserve this character note that it is U+037E if you wish to run this test.
var name = “a1234Íž”;
var response = isValidName(name);
ActiveDocument.Host.Alert(“target = ” + name + “nn” + response);[/code]