Pages: 1
Print
Author Topic: Tip: Validating the main document from a file entity; also cleaning bad PIs  (Read 1931 times)
dcramer
Member

Posts: 120


« on: December 18, 2008, 04:56:50 PM »

Out-of-the-box, XMetaL can't validate file entities because they don't have DOCTYPEs. I do the following in On_Before_Document_Validate to validate the parent doc. This looks for <?parent-doc path_to/parent.xml?>, where path_to/parent.xml is the full path to the parent document that contains the DOCTYPE. This goes in your dtd-level customization file.

You should replace "broadbook.dtd" with the filename of your DTD. IIRC, XMetaL returns the filename of the dtd as the system id for sub-documents.

There's also a function in there to remove cases where you accidentally delete the contents of a PI without deleting the PI, <??>, XMetaL doesn't catch it as a validation error but downstream processors might. Derek Read gave me this one to solve a problem I was having.

<MACRO name="On_Before_Document_Validate" lang="JScript" desc="Occurs at the time a document is valided." hide="false"><![CDATA[// XMetal Author JScript Macro File
function doCleanBadPis(){
    allPIs = ActiveDocument.getNodesByXPath("//processing-instruction()");
    for (i=allPIs.length-1; i>=0; i--) { //we're removing nodes from the tree so it's safer to do this backwards
        node = allPIs.item(i);
        // Application.Alert(i + ": " + node.nodeName);
        if (node.nodeName == "") { //check if PITarget is null
            node.parentNode.removeChild(node); //remove the node
        }
    }
}

if(ActiveDocument.ViewType == sqViewNormal || ActiveDocument.ViewType == sqViewTagsOn) {
    doCleanBadPis();
    // I can't remember why I need to do ActiveDocument.doctype.systemId == "broadbook.dtd",
    // but broadbook.dtd is the name of our dtd, tho the system id is never just the dtd name.
    // I have a vague memory that if you're in a sub-document, XMetaL returns the bare dtd name
    // as the system id. So try replacing broadbook.dtd with your dtd name.
    if(ActiveDocument.doctype.systemId == "broadbook.dtd" || ActiveDocument.doctype.systemId == ""){
        Application.StopValidation();

        var parentDoc = ActiveDocument.getNodesByXPath("//processing-instruction('parent-doc')");
        // Store the object representing
        // the active document
        var doc = Application.ActiveDocument;
       
        if(parentDoc.length != 0){
            try{
                var parentDocObj = Documents.item(ActiveDocument.Path + "\\" + parentDoc.item(0).nodeValue);
                parentDocObj.validate();
                if(parentDocObj.isValid){
                    doc.Activate();
                }else{
                    parentDocObj.Activate();
                }
            }catch(e){
                if(Application.FileExists(ActiveDocument.Path + "\\" + parentDoc.item(0).nodeValue)){
                    var parentDocObj2 = Documents.Open(ActiveDocument.Path + "\\" + parentDoc.item(0).nodeValue);
                    ActiveDocument.validate();
                    if(ActiveDocument.isValid){
                        doc.Activate();
                    }
                }
            }
        }else{
            Application.Alert("Can't validate a document without a DOCTYPE or a <?parent-doc path-to-parent?>","Sorry");
        }
    }
}else if(ActiveDocument.doctype.systemId == "broadbook.dtd" || ActiveDocument.doctype.systemId == ""){
        Application.StopValidation();
        // Warn them that validating in plain text view is a bad idea if there's no doctype.   
        Application.Alert("Validating a doc with no doctype in plain text view is pretty useless.\n I can't swtich to your parent doc even if you have a <?parent-doc ./filename.xml?>\n processing instruction  declared.\n\n Try switching to normal or tags on.");

}

                                                           
]]></MACRO>
« Last Edit: December 18, 2008, 08:28:52 PM by dcramer » Logged

David Cramer
Technical Writer
Motive, an Alcatel-Lucent Company
Pages: 1
Print
Jump to:  

email us